Ruby on rails Rails-在索引视图上显示关联数据

Ruby on rails Rails-在索引视图上显示关联数据,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我正在努力让一个模型在索引页中的分部中正确迭代 课程: app/views/chapters/index.html.erb: app/views/chapters/_chapter.html.erb: 当前代码显示下面的屏幕截图。问题是它会遍历所有的县,而不仅仅是与给定章节相关联的县 由于我在索引视图中,而不是在显示视图中,如何在分部中添加一个章节特定变量,使县基于:chapter_id字段进行迭代 class ChaptersController < ApplicationControl

我正在努力让一个模型在索引页中的分部中正确迭代

课程:

app/views/chapters/index.html.erb:

app/views/chapters/_chapter.html.erb:

当前代码显示下面的屏幕截图。问题是它会遍历所有的县,而不仅仅是与给定章节相关联的县

由于我在索引视图中,而不是在显示视图中,如何在分部中添加一个章节特定变量,使县基于:chapter_id字段进行迭代

class ChaptersController < ApplicationController
  def index
    @chapters = Chapter.all
    # @counties = County.all(:joins => :chapter, :select => "counties.*, chapters.id")
  end
end
视图:

视图:


我想这样的东西对你会有用的:

<%= @chapters.each do |chapter| %>

    <div class="row">
      <div class="span5 offset1"><h4><%= link_to chapter.name, edit_chapter_path(chapter.id) %></h4></div>
      <div class="span2"><h4><%= chapter.chapter_num %></h4></div>
    </div

    <% chapter.counties.each do |county| %>
        <div class="row">
          <div class="span4 offset1"><%= county.name %></div>
          <div class="span4 offset1"><%= county.county_num %></div>
          <div class="span2"><%= link_to 'edit', '#' %></div>
        </div>
    <% end %>
    <%= link_to "New county", new_chapter_county_path(chapter) %>
<% end %>
请注意,关键是要理解,因为每章都有许多县,所以您应该通过chapter.countries.each遍历每章的县,这将只提供属于该特定章节的县


还要注意创建新县的不同链接路径。如果您的路线设置为将县嵌套在章节下,您应该能够执行新的章节县路径章节

我认为类似的操作适合您:

<%= @chapters.each do |chapter| %>

    <div class="row">
      <div class="span5 offset1"><h4><%= link_to chapter.name, edit_chapter_path(chapter.id) %></h4></div>
      <div class="span2"><h4><%= chapter.chapter_num %></h4></div>
    </div

    <% chapter.counties.each do |county| %>
        <div class="row">
          <div class="span4 offset1"><%= county.name %></div>
          <div class="span4 offset1"><%= county.county_num %></div>
          <div class="span2"><%= link_to 'edit', '#' %></div>
        </div>
    <% end %>
    <%= link_to "New county", new_chapter_county_path(chapter) %>
<% end %>
请注意,关键是要理解,因为每章都有许多县,所以您应该通过chapter.countries.each遍历每章的县,这将只提供属于该特定章节的县


还要注意创建新县的不同链接路径。如果您的路线设置为县嵌套在章节下,您应该能够执行新的章节县路径章节

。没有什么比这更明显的东西能提醒我自己是多么的绿色。谢谢成功了。没有什么比这更明显的东西能提醒我自己是多么的绿色。谢谢我只是切换到嵌套县路线,但在发布时没有嵌套它们。您的解决方案是正确的,但我还需要从索引定义中删除@countries赋值,正如Valery Kvon在上面指出的那样。谢谢我只是切换到嵌套县路线,但在发布时没有嵌套它们。您的解决方案是正确的,但我还需要从索引定义中删除@countries赋值,正如Valery Kvon在上面指出的那样。谢谢
class ChaptersController < ApplicationController
  def index
    @chapters = Chapter.all
    # @counties = County.all(:joins => :chapter, :select => "counties.*, chapters.id")
  end
end
<% chapter.counties.each do |county| %>
<%= @chapters.each do |chapter| %>

    <div class="row">
      <div class="span5 offset1"><h4><%= link_to chapter.name, edit_chapter_path(chapter.id) %></h4></div>
      <div class="span2"><h4><%= chapter.chapter_num %></h4></div>
    </div

    <% chapter.counties.each do |county| %>
        <div class="row">
          <div class="span4 offset1"><%= county.name %></div>
          <div class="span4 offset1"><%= county.county_num %></div>
          <div class="span2"><%= link_to 'edit', '#' %></div>
        </div>
    <% end %>
    <%= link_to "New county", new_chapter_county_path(chapter) %>
<% end %>