# Drop down list?

**URL:** <https://www.ruby-forum.com/t/drop-down-list/176128>\
**Category:** Rails\
**Created:** [September 25, 2009, 2:01am UTC](https://www.ruby-forum.com/t/drop-down-list/176128 "2009-09-25T02:01:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 2:01am UTC](https://www.ruby-forum.com/t/drop-down-list/176128/1 "2009-09-25T02:01:18Z")

</div>

Hi Gurus, I’m trying to tackle a drop down list. I have category and  
item dbs, and they’re wired up with category has\_many :items and item  
belongs\_to :category, and an item has a category\_id. I have this code  
in my new/edit.html.erb for item:

\<%= f.collection\_select :category\_id,  
Category.find\_main, :id, :header, {}, :multiple =\> false %\>

And a pretty drop down list comes up, but no matter what I do, I can’t  
save an item with a different category–it always stubbornly records  
the same one (or doesn’t change it at all). The source html code  
generated is:

Awesome Things

Good Things Better Things

…and although I can “choose” a different category for an item than  
‘Good Things’, it only records ‘Good Things’ (category\_id 1) for all  
items.

Any ideas what the problem could be here?

Many TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 9:39am UTC](https://www.ruby-forum.com/t/drop-down-list/176128/2 "2009-09-25T09:39:04Z")

</div>

2009/9/25 Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> save an item with a different category–it always stubbornly records  
> items.  
> The first thing to do is to look in your log file (log/developent.log)  
> to check that the selection is being passed in params. If it is then  
> consider whether what the controller action that you are passing it to  
> is doing with it.

Colin

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/drop-down-list/176128/3 "2009-09-25T13:44:22Z")

</div>

Thanks super, Colin–that really helps. I believe category\_id is  
being passed, as in the development.log

Processing ItemsController#update (for ::1 at 2009-09-25 06:34:23)  
[PUT]  
Parameters: {“commit”=\>“Update”, … “id”=\>“228”, “item”=\>  
{…“category\_id”=\>[“2”], …}}  
[4;35;1mPage Load (1.1ms) [0m [0mSELECT \* FROM “pages” ORDER BY  
position [0m  
[4;36;1mUser Load (0.6ms) [0m [0;1mSELECT \* FROM “users” WHERE  
(“users”.“id” = 1) LIMIT 1 [0m  
[4;35;1mItem Load (0.7ms) [0m [0mSELECT \* FROM “items” WHERE  
(“items”.“id” = 228) [0m  
Redirected to [http://localhost:3000/items/228](http://localhost:3000/items/228)  
Completed in 102ms (DB: 2) | 302 Found [[http://localhost/items/228](http://localhost/items/228)]

and for different updates, the number in category\_id [] is changing.  
In my controller, I have the provided boilerplate for update:

def update  
@item = Item.find( params[:id] )

```
respond_to do |format|
  if @item.update_attributes(params[:item])

```

…

Am I missing something in the controller? Does a collection need to  
be handled separately?

Many TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/drop-down-list/176128/4 "2009-09-25T19:01:05Z")

</div>

Thanks again, Colin–in item.rb:

belongs\_to :category

And in category.rb:

has\_many :items

Item has a field category\_id which I intended to populate with the  
links from an item to its category. The field header in the category  
db is where the name of the category is.

Am I setting it up wrong then to have:

\<%= f.collection\_select :category\_id, Category.all, :id, :header,  
{}, :multiple =\> false %\>

in the form? It does show up as a drop down box with the names  
populated in it.

Again, thanks super for helping me figure this out,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 3:21pm UTC](https://www.ruby-forum.com/t/drop-down-list/176128/5 "2009-09-25T15:21:33Z")

</div>

2009/9/25 Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks super, Colin–that really helps. Â I believe category\_id is  
> being passed, as in the development.log
> 
> Processing ItemsController#update (for ::1 at 2009-09-25 06:34:23)  
> [PUT]  
> Â Parameters: {“commit”=\>“Update”, … “id”=\>“228”, “item”=\>  
> {…“category\_id”=\>[“2”], …}}

This looks a bit odd, why is it passing the id as a single element  
array I wonder?  
See below

> In my controller, I have the provided boilerplate for update:
> 
> > > Hi Gurus, I’m trying to tackle a drop down list. Â I have category and  
> > > item dbs, and they’re wired up with category has\_many :items and item  
> > > belongs\_to :category, and an item has a category\_id. Â I have this code  
> > > in my new/edit.html.erb for item:
> > 
> > > \<%= f.collection\_select :category\_id,  
> > > Category.find\_main, :id, :header, {}, :multiple =\> false %\>

I don’t see anything wrong with this.

> > > And a pretty drop down list comes up, but no matter what I do, I can’t  
> > > save an item with a different category–it always stubbornly records  
> > > the same one (or doesn’t change it at all). Â The source html code  
> > > generated is:
> > 
> > > \<option

There is the problemI think, the extra set of [] for item[category\_id].  
Are you sure you have item belongs\_to :category? It is as if it  
thinks there should be an array of id values.

Colin

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/drop-down-list/176128/6 "2009-09-25T20:52:18Z")

</div>

If I replace

f.collection\_select :category\_id, Category.all, :id, :header,  
{}, :multiple =\> false

with just

f.collection\_select :category\_id, Category.all, :id, :header

it works!

Is this a bug, or is it me being an idiot?

TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 11:56pm UTC](https://www.ruby-forum.com/t/drop-down-list/176128/7 "2009-09-25T23:56:05Z")

</div>

The weird thing is that it is working now with  
f.collection\_select :category\_id, Category.all, :id, :header instead  
of f.collection\_select :category\_id, Category.all, :id, :header,  
{}, :multiple =\> false

so :multiple =\> false seems to be the problem. But why? Is this a  
bug?

Again, many thanks,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 25, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/drop-down-list/176128/8 "2009-09-25T21:56:06Z")

</div>

2009/9/25 Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> links from an item to its category. Â The field header in the category  
> db is where the name of the category is.
> 
> Am I setting it up wrong then to have:
> 
> \<%= f.collection\_select :category\_id, Category.all, :id, :header,  
> {}, :multiple =\> false %\>
> 
> in the form? Â It does show up as a drop down box with the names  
> populated in it.

That should be fine as far as I can see, though I don’t understand the  
extra [] in the html you showed. One question, you said that it is  
not saving the new category id value, are you sure that it is not  
saving it, or is it possibly just not displaying it when you come  
round next time. Have a look in the db to see.

Another question, in an earlier post you showed the record being saved  
by update\_attributes, what are you doing if that fails? In other  
words is the save succeeding?

I am getting to the point where I don’t know what is happening I am  
afraid.

Colin

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 26, 2009, 9:39am UTC](https://www.ruby-forum.com/t/drop-down-list/176128/9 "2009-09-26T09:39:45Z")

</div>

2009/9/25 Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The weird thing is that it is working now with  
> f.collection\_select :category\_id, Category.all, :id, :header instead  
> of f.collection\_select :category\_id, Category.all, :id, :header,  
> {}, :multiple =\> false
> 
> so :multiple =\> false seems to be the problem. Â But why? Â Is this a  
> bug?

It does seem odd. If you had said :multiple =\> true then I would  
expect to see an array of ids in params, which is what you have. It  
is as if the :multiple has triggered the use of an array even though  
you have specified it as false. In fact of course using false is I  
believe redundant as this is the default. I could not find anything  
in the docs that specifies what should happen with a false setting (as  
opposed to no setting). Perhaps it is intended like this, nil means  
there will always be a single selection, true or false means it should  
be passed as an array (which is what caused your code not to work) and  
it either will actually allow multiple selections or not. There would  
be some logic in this for a user who wanted at run time to decide on  
whether multiple selections were allowed so he could use :multiple =\>  
current\_option\_setting and the code picking up the params would always  
expect an array whatever the setting of current\_option\_setting.

Colin
