我试图让它工作,但我真的无法找到问题所在。这是代码。创建评论后,我尝试在不刷新页面的情况下呈现评论。
创建.js.erb
$('.post').append("<%= escape_javascript render(@comment) %>");
评论控制器.rb
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create!(params[:comment])
respond_to do |format|
format.html
format.js
end
end
end
index.html.erb
<% @posts.each do |post| %>
<div class="post">
<p><strong> <%= post.title %> </strong>, posted <%= post.created_at.to_date %> </p> <br />
<%= post.content %> <br />
<%= simple_form_for [post, post.comments.build], :remote => true do |f| %>
<%= f.input :content %>
<%= f.button :submit %>
<% end %>
<% post.comments.each_with_index do |comment, i| %>
<p> <%=i + 1%>.<%= comment.content %></p>
<% end %>
</div>
<% end %>
<p><%= link_to "New Post", new_post_path %></p>