How do I create a multiple item select list in Rails from database



<%= f.collection_select :id, Organisation.order(:Company_Name), :id, :Company_Name, options = {include_blank: "Select an Organisation"}, html_options = {:onchange => 'broadcast_dropdown_change(document.getElementById("broadcast_country_id"), document.getElementById("broadcast_organisation_id"))'} %>

This is my code and its working fine but I want to be able to select multiple Organisations from the database instead of the dropdown list which only allows me one. Please help how I can improve this code to enable it to do so thank you.




I think you just need to add the html option multiple: true 🙂 Hopefully that’ll do it, nice and easily!

If not, you’ll need to ensure the permitted parameters match up to what’s getting passed through – likely a JSON encoded array of options – and parse / save them as appropriate in the database.

Couple more examples available in the duplicate I’ve linked in the comment to your post. Let me know how you get on and I’ll provide a little more info if need be!



<%= f.collection_select 
      Organisation.order(:Company_Name).select(:id, :Company_Name), 
      { include_blank: "Select an Organisation" },
      { onchange: 'broadcast_dropdown_change(document.getElementById("broadcast_country_id"), document.getElementById("broadcast_organisation_id"))'
      { multiple: true } %>
  • Organisation is mispelled.
  • No need to get all the Organisation attributes when you need only two.
  • Don’t use hash rocket syntax when not needed.
  • Not sure what you’re doing in the onchange attribute.
  • Use snake_case.