Simple_Form:使两个表单相互协作(下拉列表和搜索)

时间:2014-06-19 17:33:32

标签: ruby-on-rails ruby forms sorting simple-form

我有两个表单,一个包含一个下拉列表,用户可以选择列表的排序方式,另一个包含搜索字段,用户可以搜索该列表。现在,如果用户搜索" test"并且显示了十个结果,我希望用户能够从下拉列表中进行选择,结果如何排序。因此,如果他对整个列表进行排序,我希望他能够搜索列表,结果以他之前选择的排序方式显示。由于代码限制,我必须以不同的形式保留这两个输入。

这是排序下拉列表:

= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
  = f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')

这是搜索:

= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
  = f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}

是否可以保持两个输入分开,或者只使用一个表单更容易?

1 个答案:

答案 0 :(得分:0)

如果需要,您可以使用单独的表单,只需将其他参数存储在每个表单的隐藏字段中。

= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
  = f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
  = f.input :q, as: :hidden, input_html: { :name => :q, :value => params[:q] }

= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
  = f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
  = f.input :sort, as: :hidden, collection: [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')