Has many and link_to

Ok so I am building a directory that is organized by state, county, city, zip. I created tables for each of them, so i have a table for states, counties, cities, and zips. Also in each table I have a title column and a corresponding reference _id table. Meaning in my states table i have a title column and a county_id colomun. same with all the other tables.

In my models folder for state.rb I have the following

class State < ActiveRecord::Base   has_many :counties end

And in my models folder for county.rb I have the following

class County < ActiveRecord::Base   belongs_to :state   has_many :cities end

I have also generated a scaffold for state, county, city, and zip, all with a title column.

I also modified the new action for each one so I am able to select what counties belong to which states, and what cities belong to which counties etc.

I created a new display controller so I can view my new directory and it starts off with an index file that shows all the states like this

<h1>Listing States</h1>

<table> <% for state in @states %>   <tr>     <td><%= link_to state.title, {:controller => 'display', :action => 'counties'} %></td>   </tr> <% end %> </table>

And then I have another file in my display folder view called counties and it has the following in it.

<h1>Listing counties</h1>

<table> <% for county in @counties %>   <tr>     <td><%= link_to county.title, {:controller => 'display', :action => 'cities'} %></td>   </tr> <% end %> </table>

And it continues, however when u click on a particular state I only want it to show the counties that belongs to it and I do not know how to do that. Any help would be greatly appreciated. Thanks in advance.

Instead of having a display controller with actions for each of your resources, it's better to have a controller for each of your resources.

map.resources :cities, :states, :counties

link_to cities_path

More information can be found here: frozenplague.net. The tutorial covers restful routing as well as restricting results to the current object.

Yeah,nested routes can help you out

When you need a state_id, just let the user provide it in the URL!

Thank you for your replies, i checked out that link that ryan bigg suggested and it helped out a lot, thanks.