# Collection\_select not working for nested forms

**URL:** <https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874>\
**Category:** Rails\
**Created:** [February 22, 2010, 8:04am UTC](https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874 "2010-02-22T08:04:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aakash\_Dharmadhikar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@Aakash\_Dharmadhikar](https://www.ruby-forum.com/u/Aakash_Dharmadhikar)\
**Post date:** [February 22, 2010, 8:04am UTC](https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874/1 "2010-02-22T08:04:04Z")

</div>

hi guys,

Ruby 1.8.7  
Rails 2.3.5

I have a nested form in which a select drop down works fine for  
creating  
the model, but on edit form it does not show the current value.  
It gets set to the default option.

The domain is like this:  
Listing \<has\_one\> Address \<belongs\_to\> Neighbourhood

The drop down used to work for address edit form, before nesting the  
model.

address.rb → [Address.rb · GitHub](http://gist.github.com/310879)  
partial \_address\_form.html.erb → [\_address\_form.html.erb · GitHub](http://gist.github.com/310880)

The method @neighbourhood\_for\_metro in following snippet is a helper  
which  
works perfectly fine.

\<%= address\_form.label :neighbourhood %\>  
 \<%= collection\_select(:address,:neighbourood,@neighbourhood\_for\_metro,:id,:name) %\>

I spent enough time on this issue to know its some silly thing that  
I am missing.

I would really appreciate if any one could help me solve this issue.

regards,  
aakash

---

<div class="post-metadata">

**Author:** ![Aakash\_Dharmadhikar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@Aakash\_Dharmadhikar](https://www.ruby-forum.com/u/Aakash_Dharmadhikar)\
**Post date:** [February 22, 2010, 10:13am UTC](https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874/2 "2010-02-22T10:13:09Z")

</div>

Try changing the method to :neighbourhood\_id

---

<div class="post-metadata">

**Author:** ![Aakash\_Dharmadhikar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@Aakash\_Dharmadhikar](https://www.ruby-forum.com/u/Aakash_Dharmadhikar)\
**Post date:** [February 22, 2010, 11:00am UTC](https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874/3 "2010-02-22T11:00:16Z")

</div>

I have tried that, no change in behaviour.

---

<div class="post-metadata">

**Author:** ![Aakash\_Dharmadhikar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@Aakash\_Dharmadhikar](https://www.ruby-forum.com/u/Aakash_Dharmadhikar)\
**Post date:** [February 23, 2010, 5:12am UTC](https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874/4 "2010-02-23T05:12:32Z")

</div>

using  
address\_form.collection\_select(:neighbourhood\_id,@neighbourhood\_for\_metro,:id,:name)  
worked rather than collection\_select(@object,@method,…)

it seems use of form specific methods avoids a lot of trouble, when  
shifting  
forms from individual to nested.

---

<div class="post-metadata">

**Author:** ![Aakash\_Dharmadhikar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@Aakash\_Dharmadhikar](https://www.ruby-forum.com/u/Aakash_Dharmadhikar)\
**Post date:** [February 23, 2010, 5:04pm UTC](https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874/5 "2010-02-23T17:04:49Z")

</div>

Hello,

Spotted a little typo in [\_address\_form.html.erb · GitHub](http://gist.github.com/310880) on line 15: \<  
%=  
collection\_select(:address,:neighbourood,@neighbourhood\_for\_metro,:id,:name)  
%\>

That :neighbourood missing h.

Maybe theres your problem?

---

<div class="post-metadata">

**Author:** ![Aakash\_Dharmadhikar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@Aakash\_Dharmadhikar](https://www.ruby-forum.com/u/Aakash_Dharmadhikar)\
**Post date:** [February 22, 2010, 11:18am UTC](https://www.ruby-forum.com/t/collection-select-not-working-for-nested-forms/183874/6 "2010-02-22T11:18:57Z")

</div>

Any difference if you change :address to @address?

If that doesnt work, I guess you’ll have to do :selected =\>  
@address.neighbourhood\_id
