Chiperific
Chiperific

Reputation: 4696

Rails - Show associated data on Index view

I'm struggling to have a belongs_to model iterate correctly inside a partial in an index page.

Classes:

class Chapter < ActiveRecord::Base
  attr_accessible :name, :chapter_num,
  belongs_to :chapter
  #fields: :id, :name, :chapter_num
end

class County < ActiveRecord::Base
  attr_accessible :name, :county_num, :chapter_id
  has_many :counties
  #fields: :id, :name, :county_num, :chapter_id
end

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

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

<h1>Chapter Index</h1>
  <%= render @chapters %>
<br />
<%= link_to "Add a new Chapter", new_chapter_path, class: "btn btn-large btn-primary" %>

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

<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>
<!-- here's where the problem starts -->
<% @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_county_path %>
<hr>

The current code shows the screenshot below. The problem is it's iterating through all the counties, not just the counties associated with a given chapter. Screenshot of Index view

How do I add a chapter specific variable within the partial that will cause the counties to iterate based upon the :chapter_id field since I'm in the index view, not the show view?

Upvotes: 1

Views: 1219

Answers (2)

sq1020
sq1020

Reputation: 1090

I think something like this would work for you:

<%= @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 %>

Note that the key is to understand that because each chapter has many counties, you should iterate through the counties of each chapter via chapter.counties.each which will give you only the counties that belong to that particular chapter.

Also note the different link_to path for creating a new county. If you have your routes set up such that counties are nested under chapters, you should be able to do new_chapter_county_path(chapter)

Upvotes: 1

Valery Kvon
Valery Kvon

Reputation: 4496

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

View:

<% chapter.counties.each do |county| %>

Upvotes: 3

Related Questions