【问题标题】:Rails 4: Display selected value on edit in dropdownRails 4:在下拉列表中编辑时显示选定的值
【发布时间】:2015-08-23 12:32:14
【问题描述】:

我在新文章页面中有一个 f.select,它工作正常。但是,当我编辑文章时,该值会变回选项中的第一个值。

有没有办法保留选择值,以便用户在编辑他们的文章时不必担心重做?

这是我的选择标签:

<%= f.select :category, 
    options_for_select(['drink','food','medicine','supplement','drug','ingredient','lifestyle','other'], params[:category]), {}, { :class => 'span3 controls controls-row' } %>

【问题讨论】:

    标签: html ruby-on-rails forms ruby-on-rails-4 select


    【解决方案1】:

    变化:

    <%= f.select :category,   options_for_select(['drink','food','medicine','supplement','drug','ingredient','lifestyle','other'], params[:category]), {}, { :class => 'span3 controls controls-row' } %>
    

    到:

    <%= f.select :category, options_for_select(['Mare', 'Stallion', 'Gelding'], :selected => f.object.category), {}, { :class => 'span3 controls controls-row' } %>
    

    类似于这里的:https://stackoverflow.com/a/19120874/3507417

    【讨论】:

      【解决方案2】:

      您应该在options_for_select(&lt;...&gt;) 中提供selected: :category 而不是params[:category]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        • 2020-05-14
        • 2017-12-02
        • 2021-09-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多