0

我的评论应用程序有效,但唯一的问题是每当我刷新页面时评论就会消失。在日志中它显示正文已插入评论表中(已保存)。我在这里做错了什么?任何帮助将不胜感激。先感谢您。

查看#show

    <div id="comments"></div>

   <%= form_for :comment,:remote => true,:url=>  {:controller=>"comments",:action=>"create"},:html => { :id => 'new-comment'} do |f| %>
   <%= f.text_area(:body) %>
   <div class="errors"></div>
   <%= f.submit "post" %>
   <% end %>

评论控制器

    class CommentsController < ApplicationController
    respond_to  :js
   def create
   @deal=Deal.find(1)
   @comment =@deal.comments.build(params[:comment])
   @comment.save
   respond_with( @comment, :layout => !request.xhr? )  
   end

    def show
    @comment=Comment.all
    @deal=Deal.find(1)
    @comment=@deal.comments
    end
   end

创建.js.erb

     $('#comments').append("escape_javascript(@comment.body)");
4

2 回答 2

1

我看不到您的评论在您的节目模板中显示的位置。

这样的事情怎么样?

   <div id="comments">
      <% @comments do |comment| %>
        <%= comment.body %>
      <% end %>
   </div>

   <%= form_for :comment,:remote => true,:url=>  {:controller=>"comments",:action=>"create"},:html => { :id => 'new-comment'} do |f| %>
     <%= f.text_area(:body) %>
      <div class="errors"></div>
     <%= f.submit "post" %>
   <% end %>

请注意,您需要@comments在控制器中设置或使用其他获取评论的方法,例如@view = View.find(params[:id])<%= @view.comments.each do |comment| %>...

于 2011-10-11T20:48:10.390 回答
0

我猜评论与未分配的帖子有belongs_to关系。

在您的表格中,您应该添加

<%= f.hidden_field :post_id, @post.id %>

如果您想按书行事, post_id 应该是 attr_protected ,而是在评论控制器中手动分配

@comment = Comment.new(params[:comment])
@comment.post_id = params[:comment][:post_id]
@comment.save
于 2011-10-11T20:37:54.450 回答