Javascript Rails使用submit解析Ransack参数
我有一个Rails应用程序,使用ruby 2.3.3 对于我的表单,我使用简单的表单,搜索过滤并将分页 我有“每页设置”下拉列表,因此用户可以选择是否希望在表中看到20、200或200篇文章Javascript Rails使用submit解析Ransack参数,javascript,ruby-on-rails,ruby,url-parameters,Javascript,Ruby On Rails,Ruby,Url Parameters,我有一个Rails应用程序,使用ruby 2.3.3 对于我的表单,我使用简单的表单,搜索过滤并将分页 我有“每页设置”下拉列表,因此用户可以选择是否希望在表中看到20、200或200篇文章 = form_tag({ action: "index"}, { method: "get", id: "post_index"}) do =select_tag :per_page, otions_for_select( [20,200,2000], params[:per_page].to_i )
= form_tag({ action: "index"}, { method: "get", id: "post_index"}) do
=select_tag :per_page, otions_for_select( [20,200,2000], params[:per_page].to_i )
我的javascript是这样的:
$(document).ready(function() {
$("#per_page").change(function() {
$("#post_index").submit();
});
});
http://localhost:3001/posts?q[name_eq]=Foo&q[title_eq]=Bar&per_page=200
= form_tag({ action: "index"}, { method: "get", id: "post_index"}) do
=select_tag :per_page, otions_for_select( [20,200,2000], params[:per_page].to_i )
=hidden_field_tag :q, params[:q] if params[:q].present?
因此,当用户更改下拉列表时,它将再次呈现操作“索引”,并将每页参数设置为选定值。到目前为止还不错,但当我提交搜索参数时就不见了
当我使用Ransack进行筛选时,URL为:
http://localhost:3001/posts?q[name_eq]=Foo&q[title_eq]=Bar
当我选择“每页”下拉列表时,我的URL将变成:
http://localhost:3001/posts?per_page=200
如何将Ransack调用的参数解析为per_页面调用。因此,我将有一个如下URL:
$(document).ready(function() {
$("#per_page").change(function() {
$("#post_index").submit();
});
});
http://localhost:3001/posts?q[name_eq]=Foo&q[title_eq]=Bar&per_page=200
= form_tag({ action: "index"}, { method: "get", id: "post_index"}) do
=select_tag :per_page, otions_for_select( [20,200,2000], params[:per_page].to_i )
=hidden_field_tag :q, params[:q] if params[:q].present?
编辑:
我试图在一个隐藏字段中添加Ransack参数,如下所示:
$(document).ready(function() {
$("#per_page").change(function() {
$("#post_index").submit();
});
});
http://localhost:3001/posts?q[name_eq]=Foo&q[title_eq]=Bar&per_page=200
= form_tag({ action: "index"}, { method: "get", id: "post_index"}) do
=select_tag :per_page, otions_for_select( [20,200,2000], params[:per_page].to_i )
=hidden_field_tag :q, params[:q] if params[:q].present?
但这会导致一个错误:
undefined method `merge' for "#<ActionController::Parameters:0x175c6110>":String
“#”的未定义方法“merge”:字符串
您需要在ransack表单中添加隐藏标签,更新其值,并在更改时提交 将隐藏标记添加到表单:
= semantic_search_form_for(@q) do |f|
...
= f.hidden_field :per_page, value: params[:q][:per_page] || 20
...
将选择与每页放置在任意位置:
= select_tag :per_page, options_for_select(["20", "200", "2000"], params[:q][:per_page])
添加脚本:
javascript:
$('#per_page').on('change', function() {
$('#q_per_page').val(this.value);
$('#search').submit();
});
根据迈克尔·马洛夫的回答,我创造了这个: 在搜掠搜索表中,我添加了:
= search_form_for @q do |f|
= f.hidden_field :per_page, value: @per_page
javascript现在是:
$('#per_page').on('change', function() {
$('#q_per_page').val(this.value);
$('#search').submit();
});
现在,下拉列表为:
= form_tag({ action: "index"}, { method: "get", id: "post_index"}) do
=select_tag :per_page, otions_for_select( [20,200,2000], @per_page )
在控制器内部,我有:
@per_page = params[:q].present? ? params[:q][:per_page] : 20
这可能是一个选项,但我有一个布局,其中搜索过滤在一个模式中。“每页”下拉列表就在表格上方。我知道它必须以某种方式工作,我不必改变布局来让它工作。现在我明白了!美好的将用anwser更新我的问题。谢谢