hardow2011
hardow2011

Reputation: 59

Using select2's selected option value in a rails forms field

How do I assign the selected option value:

<select class="select2-simple-dropdown">
    <% Season.all.each do |season| %>
        <option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
    <% end %>
</select>

To a form's field, let's say: Voyage.given_season ?

Upvotes: 1

Views: 1570

Answers (2)

raj_acharya
raj_acharya

Reputation: 665

If you want your select input to accept multiple options, you can pass multiple: true

<%= f.select(:season_id,  Season.all.collect {|m| [ m.name, m.id] }, class: "form-control select2-simple-dropdown", id: "list-markets", multiple: true) %>

https://aalvarez.me/posts/select2-with-simple-form-in-rails/

Upvotes: 1

Rishabh Aggarwal
Rishabh Aggarwal

Reputation: 83

Use the rails select field instead and do it like this

<%= f.select :season_id, Season.all.pluck(:name, :id), {},
                         { class: 'select2-simple-dropdown'}  %>

Hope this helps.

Upvotes: 1

Related Questions