Question

I am currently using Kaminari for pagination, and I am aware of the link_to_next_page method. However, I am currently looking to "link to the next page" within the pagination scope after a form has been processed and submitted.

In other words:

  1. The user clicks the "submit" button
  2. The form is submitted via :remote => true
  3. Javascript programmatically directs the user to the next page.

Sort of like: create.js.erb

  <% if @result %>
    <script type="text/javascript">
      window.location.href="<%= @user.next_page %>
    </script>
  <% end %>

Is it possible for me to generate/determine what the next page is and then redirect the user via javascript?

Thanks!

Final solution: Based on Kieran's help below, here's what I ended up doing:

<% if @result %>   
  <% unless @users.last_page? %>
  window.location.href="?page=<%=@users.current_page+1 %>"
  <% else %>
      window.location.href="completed"
<% end %>

Which appears to work just fine for me.

Was it helpful?

Solution

The problem you have is that Kaminari requires the "scope" in order to correctly ascertain the next page from your current page:

https://github.com/amatsuda/kaminari/blob/bc51fd08f9e67b8338b421f6827d7d8e54c82deb/lib/kaminari/helpers/action_view_extension.rb#L64

Since you are submitting to the javascript, you will need to provide this scope to this method to make it work. In your controller in your create method you will need to have the same code to query the object that you want paginated on and then let Kaminari do the rest.

@users = User.order(:name).page params[:page]

You will need to ensure that the form also sends along the params[:page]

Then you can do:

  <% if @result && !@users.last_page? %>
    <script type="text/javascript">
      window.location.href="<%=  params.merge(param_name => (@users.current_page + 1)) %>"
    </script>
  <% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top