Params from a collection_select

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

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]

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

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]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs