在 rails 表单字段中使用 select2 的选定选项值
Using select2's selected option value in a rails forms field
如何分配选定的选项值:
<select class="select2-simple-dropdown">
<% Season.all.each do |season| %>
<option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
<% end %>
</select>
对于表单的字段,假设:Voyage.given_season
?
改用 rails select 字段并按如下方式进行
<%= f.select :season_id, Season.all.pluck(:name, :id), {},
{ class: 'select2-simple-dropdown'} %>
希望对您有所帮助。
如果你想让你的 select 输入接受多个选项,你可以传递 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/
如何分配选定的选项值:
<select class="select2-simple-dropdown">
<% Season.all.each do |season| %>
<option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
<% end %>
</select>
对于表单的字段,假设:Voyage.given_season
?
改用 rails select 字段并按如下方式进行
<%= f.select :season_id, Season.all.pluck(:name, :id), {},
{ class: 'select2-simple-dropdown'} %>
希望对您有所帮助。
如果你想让你的 select 输入接受多个选项,你可以传递 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/