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=State.find_or_create_by_name(params[:state])   @state.country_id =

  @city=City.create(params[:city])   @city.country_id =   @city.state_id =

  @category=Category.find_or_create_by_name(params[:category])   @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 model.

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) end

* 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 trouble.

Example 1 - Creating a state:

def state   @state=State.create(params[:state])   @state.country_id =   redirect_to :action =>'list' end

Should be:

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

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 point.

What about this? In your main form have a drop down list *and* a text field. 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?