Search code examples
ruby-on-railssimple-formcollection-select

How to show more attributes in each element of the collection with collection_select


I have this with simple form:

= simple_form_for(Note.new, remote: true) do |f|       
  = f.collection_select :dance_id, current_user.dances.includes(:style).all,     
   :id, :style_name, {prompt: "Please select dance"}, {class: "form-control"}

Code generated:

<select class="form-control" name="note[dance_id]" id="note_dance_id">
    <option value="">Please select dance</option> 
    <option value="39">Adowa Dance (Ghana)</option> 
    <option value="38">Adowa Dance (Ghana)</option> 
    <option value="37">Tango (Argentina)</option>
</select>

which works fine as it is displaying all dances by style_name as i want it. But i want to make a difference between two dances and want to also show level and date right next to each dance in the collection because there are many dances with same name bc of the style which is the same! please help. any better way to do it with simple form helpers?


Solution

  • As stated in the discussion, you needed to declare a method in your model's rb file like this:

    def custom_name
    "#{style.name}. #{date}"
    end
    

    And change the :style_name parameter to :custom_name