problem with id's

I am not sure I follow, but:

There is no :id being passed to the controller from the form.

The create method could be:

def create
  @country=Country.find_or_create_by name(params[:country])

  @state.country_id =

  @city.country_id =
  @city.state_id =

  @category.country_id =
  @category.state_id =
  @categort.city_id =

  redirect_to :action =>'list'


Anyone have another way?


Move the SQL logic out of your view - it's bad practice. Much more
readable if its in the controller, or even better pushed down to the

Try this in the list view:

<%= form_tag :action => "update" do %>

  <h4>Select Country<br>
  <% @countries.each do |country| %>
    <%= select('name', country) %><br>
  <% end %>

  <h4>Select State<br>
  <% @states.each do |state| %>
    <%= select('name', state) %>
  <% end %>

  <h4>Select City<br>
  <% @cities.each do |city| %>
    <%= select('name', city) %><br>
  <% end %>

  <h4>Select Category<br>
  <% @categories.each do |category| %>
    <%= select('name', category) %><br>
  <% end %>

  <%= submit_tag %>

<%= end %>

and in the controller:
def list
  @countries = Country.find(:all)
  @states = State.find(:all)
  @cities = City.find(:all)
  @categories = Category.find(:all)

* The list view code may not work and it's ugly, but you get the idea.
Can anyone tell us if you can do something like:

<%= select('name', @categories) %>


OK, so you have the same basic problem iterated a few times.

I began to type a few answers to this problem, but kept coming into

Example 1 - Creating a state:

def state
  @state.country_id =
  redirect_to :action =>'list'

Should be:

def state
  @state.country_id = Country.find_by_name(params[:country]).id
  redirect_to :action =>'list'

BUT when the final form is submitted it will create YET ANOTHER state
in the table....

OK, how about we create some dynamic drop down boxes that change
content based on previous selections?
Doable, but a little messy and probably a bit too much effort at this

What about this? In your main form have a drop down list *and* a text
The user can either select the country/state/city/category from the
list, and if they do not find it can type one in.
Then check for the values submitted and act appropriately:
no drop down selected, no text = throw an error
drop down, no text = use drop down value
drop down + text = text overrides dropdown(?? or vice versa?) <= User
must be informed prior
no drop down + text = Use text.

Along the right track?