我在一个页面上使用了几个search_form_for,我在传递参数时遇到了问题。当提交其中一个表单时,其参数将覆盖其他表单的参数。
例如,在提交时需要这个:
Parameters: {"q"=>{"search_cont"=>"qwerty", "by_discarded"=>"discarded"}, "f"=>{"search_cont"=>"asdfgh", "by_discarded"=>"active"}}
但要得到它
Parameters: {"q"=>{"search_cont"=>"qwerty", "by_discarded"=>"discarded"}}
或者它
Parameters: {"f"=>{"search_cont"=>"asdfgh", "by_discarded"=>"active"}}
- 浏览次数 *:
<%= search_form_for @q, url: url, as: :q do |f| %>
<%= f.search_field :search_cont, value: params.dig(:q, :search_cont), placeholder: 'Search' %>
<%= f.select(:by_discarded, [['Active', 'active'], ['Discarded', 'discarded']], { selected: params.dig(:q, :by_discarded) }, { onchange: 'this.form.submit();' }) %>
<% end %>
...
<%= search_form_for @f, url: url, as: :f do |f| %>
<%= f.search_field :search_cont, value: params.dig(:f, :search_cont), placeholder: 'Search' %>
<%= f.select(:by_discarded, [['Active', 'active'], ['Discarded', 'discarded']], { selected: params.dig(:f, :by_discarded) }, { onchange: 'this.form.submit();' }) %>
<% end %>
- 控制器 *:
@q = some_items.ransack(params[:q])
@q.sorts = 'sort_order asc' if @q.sorts.empty?
@pagy_q, @objects_q = pagy(@q.result, page_param: :page_q)
@f = another_items.ransack(params[:f], search_key: :f)
@f.sorts = 'sort_order asc' if @f.sorts.empty?
@pagy_f, @objects_f = pagy(@f.result, page_param: :page_f)
如何在提交表单时传递所有表单的参数?
1条答案
按热度按时间wwwo4jvm1#
你有几个选择:
1.使用js捕获提交事件,然后合并两个表单的参数
1.将两种形式合二为一,但在视觉上将它们分成两部分。当您提交表单时,您将只能从您提交的表单中获取参数。