Rails 3-使用Ajax和jquery更新div内容(嵌套资源)

Rails 3-使用Ajax和jquery更新div内容(嵌套资源),jquery,ruby-on-rails,ajax,ruby-on-rails-3,nested,Jquery,Ruby On Rails,Ajax,Ruby On Rails 3,Nested,我有两个简单的模型,Pin和Comment,Comments属于Pin: class Pin < ActiveRecord::Base has_many :comments, dependent: :destroy class Pin

我有两个简单的模型,Pin和Comment,Comments属于Pin:

class Pin < ActiveRecord::Base
    has_many :comments, dependent: :destroy
class Pin

class注释
在Pin的索引操作中,我基本上有一个所有Pin+adiv的列表。 此div必须在用户选择pin时显示所有注释

这个概念很简单,但我不知道如何实现它。 我发现很多科目都是相关的,但我总是沉浸在与问题的不同中

一些确切的问题:

  • 如何进行ajax加载?(使用jquery)
  • 我需要使用部分注释还是使用注释的索引视图

我将使用一个链接让用户选择Pin,但您明白了:

#:remote => true allows ajax stuffz.
<%= link_to 'show comments', pin_comments_path(@pin), :remote=> true %>
在这种情况下,控制器将查看呈现pin_comments.js.erb,它将与您的comments div交互

//pin_comments.js.erb
$("#comments_div").html("<%= j(render('show_comments', :comments=> @comments)) %>");
//pin_comments.js.erb
$(“#comments_div”).html(“@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>
##_show_comments.html.erb

通过

希望有帮助

//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>