【问题标题】:multipleselect is forgeting selected values after proceed多项选择在继续后忘记选择的值
【发布时间】:2016-03-10 14:10:47
【问题描述】:

Multiplyselect 在搜索后会忘记所有者的值。 继续后,我得到了 params[:search] 和 params[:owners] 但只填写了搜索输入。这是我的代码。

def index
 @all_owners = Owner.select('distinct name').pluck(:name)
 @animal = Animal.search(params[:search])
 @animal = @animals.joins(:owners).where("owners.name IN (?) ", params[:owners].present? ? params[:owners] : @owners)
end

#------------------------------------------

<%= form_tag animals_path, :method => 'get' do %>
 <%= text_field_tag :search, params[:search]%>
 <%= select_tag :owners, options_for_select(@all_owners),id: "multiselect-id", multiple: true %>
<%= submit_tag "Search", :name => nil  %>
<% end %>

<% @aminals.each do |animal| %>
 <%= animal.name %>
 <%= animal.owners.map(&:name).join(', ') %>
<% end %>

<script type="text/javascript">
 $(document).ready(function() {
    $('#multiselect-id').select2();
 });
</script>

【问题讨论】:

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


    【解决方案1】:

    您忘记在select_tag 中指定当前选择的值。这是完成的,例如通过 options_for_select 助手的第二个参数,例如:options_for_select(@all_owners, params[:owners] || @owners)

    请参阅docs here

    【讨论】:

    • 谢谢!我之前尝试过这样的事情: 并没有抛出错误。将 params[:owners] 替换为 options_for_select 解决了这个问题。解决方案:
    猜你喜欢
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    相关资源
    最近更新 更多