我试图让这个工作,但我真的无法解决问题所在。这是代码。 我创建它们后,尝试在不刷新页面的情况下呈现注释。
create.js.erb
$('.post').append("<%= escape_javascript render(@comment) %>");
comments_controller.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>