【问题标题】:Using select2's selected option value in a rails forms field在 Rails 表单字段中使用 select2 的选定选项值
【发布时间】:2020-03-12 06:08:38
【问题描述】:

如何分配选定的选项值:

<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 ?

【问题讨论】:

    标签: ruby-on-rails ruby jquery-select2


    【解决方案1】:

    改用 rails 选择字段并像这样进行操作

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

    希望这会有所帮助。

    【讨论】:

    • 不需要.all
    • @Int'lManOfCodingMystery 也是对的,没有.all 也可以。
    【解决方案2】:

    如果您希望您的选择输入接受多个选项,您可以传递多个: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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 2018-03-27
      • 1970-01-01
      • 2019-08-24
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多