Webdevotion.be

Developer for the Flash Platform

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 ) %>



Categorized as Ruby On Rails

5 Comments

  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. where do you write

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

Leave a Reply