# Params from a collection\_select

**URL:** <https://www.ruby-forum.com/t/params-from-a-collection-select/75225>\
**Category:** Rails\
**Created:** [November 8, 2006, 10:27am UTC](https://www.ruby-forum.com/t/params-from-a-collection-select/75225 "2006-11-08T10:27:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dominic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dominic](https://www.ruby-forum.com/u/dominic)\
**Post date:** [November 8, 2006, 10:27am UTC](https://www.ruby-forum.com/t/params-from-a-collection-select/75225/1 "2006-11-08T10:27:47Z")

</div>

from the API:

* * *

collection\_select(object, method, collection, value\_method, text\_method,  
options = {}, html\_options = {})

* * *

So how would one get the params of this collection\_select?

Thanks

---

<div class="post-metadata">

**Author:** ![dominic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dominic](https://www.ruby-forum.com/u/dominic)\
**Post date:** [November 8, 2006, 12:18pm UTC](https://www.ruby-forum.com/t/params-from-a-collection-select/75225/2 "2006-11-08T12:18:40Z")

</div>

no idea what you want … i#ll just give an example:

\<%= collection\_select :post, :category, @categories, :id, :name,  
{:include\_blank =\> true } %\>

will generate

Diary Technical Rants
- object + method form the name=“post[category]”

- the collection has the input for the option tags.

- value\_method is the method to use on the collection items to get the  
value for value=""  
here: @categories.id

- text\_method is the method to use on the collection items to get the  
display text for the options  
here: @categories.name

- the options hash take the options as explained in the API docs of  
FormOptionsHelpers, e.g. the :include\_blank i used above to create an  
empty tag

- the html options hash takes the usua stuff like :class =\> “something”  
etc…

when the form is submitted, you can access the selected value with  
params[:post][:category]

---

<div class="post-metadata">

**Author:** ![dominic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dominic](https://www.ruby-forum.com/u/dominic)\
**Post date:** [November 9, 2006, 2:49am UTC](https://www.ruby-forum.com/t/params-from-a-collection-select/75225/3 "2006-11-09T02:49:16Z")

</div>

You should write a book. Nobody explained it like this before.  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![dominic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dominic](https://www.ruby-forum.com/u/dominic)\
**Post date:** [February 25, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/params-from-a-collection-select/75225/4 "2007-02-25T18:29:15Z")

</div>

Can I just say that I have spent the last day or so working this out,  
and you solved it for me.

In my controller, I have been trying to use:

@shipping\_cost = ShippingCost.find(params[:shipping\_cost][:id])

and all I get is an issue of:

Couldn’t find ShippingCost with ID=

So now I switched to the form of:

@shipping\_cost = params[:shipping\_cost][:id]

and it works fine.

The only problem is I dont know why. Do you know why your method works  
and my original one does not?

Thanks in advance!!!

Thorsten L wrote:

> no idea what you want … i#ll just give an example:
> 
> \<%= collection\_select :post, :category, @categories, :id, :name,  
> {:include\_blank =\> true } %\>
> 
> will generate
> 
> Diary Technical Rants
> - object + method form the name=“post[category]”
> 
> - the collection has the input for the option tags.
> 
> - value\_method is the method to use on the collection items to get the  
> value for value=""  
> here: @categories.id
> 
> - text\_method is the method to use on the collection items to get the  
> display text for the options  
> here: @categories.name
> 
> - the options hash take the options as explained in the API docs of  
> FormOptionsHelpers, e.g. the :include\_blank i used above to create an  
> empty tag
> 
> - the html options hash takes the usua stuff like :class =\> “something”  
> etc…
> 
> when the form is submitted, you can access the selected value with  
> params[:post][:category]
