所以我有這樣的形式,請求JS調用時評論提交:爲什麼這個ajax請求用新提交的評論替換我的所有評論?
<%= simple_form_for([@video, @video.comments.new], :remote => true) do |f| %>
<%= f.association :comment_title, :collection => @video.comment_titles, :label => "Comment Title:", :include_blank => false %>
<%= f.input :body, :label => false, :placeholder => "Post a comment." %>
<%= f.button :submit, :value => "Post" %>
<% end %>
它調用此創建的意見控制器動作:
def create
@comment = @video.comments.new(params[:comment].merge({:user_id => current_user.id}))
if @comment.save
respond_to do |format|
format.html { redirect_to :back }
format.js
end
else
respond_to do |format|
format.html { redirect_to :back, :alert => "Unable to add comment." }
format.js { render 'fail_create.js.erb' }
end
end
end
這使得該create.js.erb文件:
$(".comment_content").html('<%= escape_javascript(render(@comment)) %>');
$(".comment_form")[0].reset();
上面create.js.erb文件呈現的註釋部分到該文件:
<% comment_title.comments.each do |comment| %>
<div class="comment_content">
<%= render comment %>
</div>
<% end %>
爲什麼我的所有評論都被這個AJAX請求中新提交的評論取代?
這裏是我的評論部分:
<%= link_to image_tag(comment.user.profile.photo.url(:tiny)), profile_path(comment.user.profile), :class => "comment_image" %>
<div class="textual_comment_content">
<div class="comment_text">
<span class="name_link">
<%= link_to "#{comment.user.name}", profile_path(comment.user.profile), :class => "normal" %>
</span>
<%= comment.body.gsub("'",''').html_safe %>
</div>
<span class="comment_footer">
<ul>
<li class="list_style"><%= time_ago_in_words(comment.created_at) %> ago</li>
<% unless current_user != comment.user %>
<li><%= link_to "Delete", video_comment_path(:video_id => @video, :id => comment), :method => :delete, :class => "normal" %></li>
<% end %>
</ul>
</span>
</div>
嘗試使用'append()'/'prepend()'而不是'html()'? – Khez 2011-04-07 17:58:36
只是這樣做不起作用......但也許如果一些其他的事情也改變了...... – 2011-04-07 18:01:05
你是什麼意思,它不工作? – Khez 2011-04-07 18:13:58