Search code examples
ruby-on-railsselectform-helpersgrouped-collection-select

multiple values combined in select helper rails


This gives me a select box with only the first_name as the value. I'd like to combine both the first_name and the last_name to display the full name as a value.

I'm thinking I'll have to create a variable to combine these and then insert this variable into my code. But anything else I put as a replacement of first_name only errors.

<%= f.grouped_collection_select :submitter_id, Building.order(:name), :submitters, :name, :id, :first_name, :prompt => "Select name"%>

Solution

  • You can do it by simply making a method in your model.

    In your model (Building or Submitter?), add the following:

    def full_name
      self.first_name + self.last_name
    end
    

    then use this method in the grouped collection helper method like:

    <%= f.grouped_collection_select :submitter_id, Building.order(:name), :submitters, :name, :id, :full_name, :prompt => "Select name"%>