Rails howto: custom value field in collection_select

Very easy this one:

Contact.rb ( Model )

...
def name_and_company
@name_and_company = name + " - " + contact.company.name
end
...

Usage ( note the use of :name_and_company )

<%= form.collection_select(:contact_id, @contacts, :id, :name_and_company ) %>

5 thoughts on “Rails howto: custom value field in collection_select

  1. This doesn’t work.

    View:

    Controller:

    def custom_name
    @custom_name = ‘test’
    end

    Error:

    undefined method `custom_name’ for #

  2. It works nevertheless.
    I confused Model with Controller.
    Sorry for the noise…

  3. Sorry for the delay in my response Michael. Cool to see this post has helped you.

  4. form.collection_select(:contact_id, @contacts, :id, :name_and_company )

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>