我将使用一个链接让用户选择一个 Pin,但你明白了:
#:remote => true allows ajax stuffz.
<%= link_to 'show comments', pin_comments_path(@pin), :remote=> true %>
在 comments_controller.rb 中(我在这里使用 index 操作,但可以根据您的需要进行调整)
def index
@pin = Pin.find(params[:pin_id])
@comments = @pin.comments.all
respond_to do |format|
format.js { render :pin_comments }
end
end
在这种情况下,控制器将渲染 pin_comments.js.erb,它将与您的评论 div 交互。
//pin_comments.js.erb
$("#comments_div").html("<%= j(render('show_comments', :comments=> @comments)) %>");
查看部分模板以显示评论
#_show_comments.html.erb
<div id="comments_div">
<% comments.each do |c| %>
<p>
<h1><%= c.title %></h1>
<h6>by <%= c.author %> </h6>
<%= c.content %>
</p>
<% end %>
</div>
希望有帮助!