gpt4 book ai didi

javascript - 多重选择在继续后忘记选择的值

转载 作者:行者123 更新时间:2023-11-27 23:14:39 25 4
gpt4 key购买 nike

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>

最佳答案

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

请参阅docs here .

关于javascript - 多重选择在继续后忘记选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35918808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com