# Ajaxy select boxes and existing records

**URL:** <https://www.ruby-forum.com/t/ajaxy-select-boxes-and-existing-records/176765>\
**Category:** Rails\
**Created:** [October 6, 2009, 12:20am UTC](https://www.ruby-forum.com/t/ajaxy-select-boxes-and-existing-records/176765 "2009-10-06T00:20:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [October 6, 2009, 12:20am UTC](https://www.ruby-forum.com/t/ajaxy-select-boxes-and-existing-records/176765/1 "2009-10-06T00:20:08Z")

</div>

I’ve just made my first form using select boxes that update each other  
via observe\_field. It works really well for the creation of record and  
it’s making the form a whole lot nicer to use.

The problem is editing records, the first select box is not part of the  
model but is used to populate the second select, which is a required  
attribute of the model. When the edit form is displayed, the second box  
is blank (as the first needs to be selected) and so you have to manually  
change it before you edit anything else and submit the form.

I’m hoping there’s some clever trick I haven’t even thought of that  
could make this work. Ideally, when the form is displayed, id like it to  
get set the first select box based on the contents of the second, and  
then set the second to the models attribute.

I’m not sure if I’m explaining this very well so please don’t hestitate  
to shout for more info. I’d be very grateful to know how to get around  
this.

Thanks

Matt

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [October 8, 2009, 8:01pm UTC](https://www.ruby-forum.com/t/ajaxy-select-boxes-and-existing-records/176765/2 "2009-10-08T20:01:57Z")

</div>

Matt H. wrote:

> I’m hoping there’s some clever trick I haven’t even thought of that  
> could make this work. Ideally, when the form is displayed, id like it to  
> get set the first select box based on the contents of the second, and  
> then set the second to the models attribute.
> 
> I’m not sure if I’m explaining this very well so please don’t hestitate  
> to shout for more info. I’d be very grateful to know how to get around this.

I’ve managed to solve this, just in case anyone else comes looking for  
this solution:

I changed the edit form so that it populates the first select box  
normally and sets it up to update the second box, but made it set  
:selected on the right entry.

Then I populated the second box initially with only the relevant entries  
and set :selected on the correct one.

Now, the form can be submitted without manually selecting the right  
entries, but if the user wishes to change them, ajax will still work.

I know the explanation of the problem or the solution isn’t very good,  
but I hope this might help someone in the same situation.

Matt
