Question : Ruby on Rails: Dynamic Selection Lists in a Form

I am trying to find the best approach to a selection list in a form that I have.

What I have are two selection lists, 'A' & 'B'.  In my database all I store is selection list 'B'.  Selection list 'A' is merely a way of organizing all the possible 'B's into category groups.  All the 'B's have a foreign key pointing back to their 'A' parent.

I need to do two things:
1) make an AJAX call to update all the 'B's when a new 'A' is selected.

2) When someone loads the record into the form for re-editing it, I need to be able to properly select the right 'A' in the list to match which 'B' the record is set to.

I am not sure where to start with step 2.  

As for step 1, I am wondering what is the best way to have a second form call inside another form.  Wouldn't that cause a conflict?  Would I have to use an 'observer' in this case?

I am including the code which I am starting with using country and state as an example.  in this case all I want to store is the state info and then be able to properly set the state and country lists when someone wants to edit it.

Thanks,
Grant
Code Snippet:
1:
2:
<%= select("any", "country", @country.collect {|c| [ c.name, c.id ] }, {:include_blank => false}) %>
<%= select("entry", "state_id", @state.collect {|s| [ s.name, s.id ] }, {:include_blank => false}) %>
Open in New Window Select All

Answer : Ruby on Rails: Dynamic Selection Lists in a Form

Dangit, I didn't mean to submit that yet.

Now, you don't have to use the select() helper.  I don't really like to use it in cases where it doesn't refer to the model being edited.

Have you looked at option groups?  Maybe your list is too long, but they're a nice way to categorize options like that.

If you don't want to use option groups, I'd just do it like this:
1:
2:
3:
4:
5:

 
# the country with an id matching the state's country_id will be selected by default
Open in New Window Select All
Random Solutions  
 
programming4us programming4us