3

I have a Rails app and using ruby 2.3.3.

For my forms I use simple_form, Ransack for the filtering and will_paginate.

I have 'set_per_page' dropdown,so Users can select if the want to see 20, 200 or 200 posts in the table.

= 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 )

My javascript is like this:

$(document).ready(function() {
  $("#per_page").change(function() {
    $("#post_index").submit();
  });
});

So when a user changes the dropdown, it will render the action 'index' again and sets the per_page parameter to the selected value. So far so good, but when I submit the Ransack parameters are gone.

When I filter with Ransack, the URL is:

http://localhost:3001/posts?q[name_eq]=Foo&q[title_eq]=Bar

When I select the 'per_page' dropdown, my URL will become:

http://localhost:3001/posts?per_page=200

How can I parse the params of the Ransack call, into the per_page call. So I will have a URL like this:

http://localhost:3001/posts?q[name_eq]=Foo&q[title_eq]=Bar&per_page=200

Edit:

I tried to add the Ransack parameters in a hidden field like this:

= 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?

But this results in a error:

undefined method `merge' for "#<ActionController::Parameters:0x175c6110>":String
Jerry
  • 198
  • 15

2 Answers2

3

You need to add hidden_tag to ransack form and update it's value and submit on change

Add hidden tag to your form:

= semantic_search_form_for(@q) do |f|
    ...
    = f.hidden_field :per_page, value: params[:q][:per_page] || 20
    ...

Place select with per_page anywhere:

= select_tag :per_page, options_for_select(["20", "200", "2000"], params[:q][:per_page])

Add script:

javascript:
$('#per_page').on('change', function() {
    $('#q_per_page').val(this.value);
    $('#search').submit();
});
Michael Malov
  • 1,877
  • 1
  • 14
  • 20
  • That could be an option, but I have a layout where the Ransack filtering is inside a modal. The 'per_page' dropdown is just above the table. I know it has to work somehow that I don't have to change the layout to get it work. – Jerry Jun 27 '17 at 19:33
  • Now I get it! Nice! Will update my question with the anwser. Thanks! – Jerry Jun 27 '17 at 19:48
2

With Michael Malov's response I created this:

Inside the Ransack searchform I added:

= search_form_for @q do |f|
  = f.hidden_field :per_page, value: @per_page

The javascript is now:

$('#per_page').on('change', function() {
  $('#q_per_page').val(this.value);
  $('#search').submit();
});

The dropdown is now:

= form_tag({ action: "index"}, { method: "get", id: "post_index"}) do
  =select_tag :per_page, otions_for_select( [20,200,2000], @per_page )

Inside the controller I have:

@per_page = params[:q].present? ? params[:q][:per_page] : 20
Jerry
  • 198
  • 15