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

Listing States

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

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

Listing counties

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

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:
http://frozenplague.net/2008/01/06/restful-routing-an-overview/
. 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs