简体   繁体   English

Rails 4-使用Kaminari使用Java重定向到下一页

[英]Rails 4 - Redirect to Next Page with Javascript using Kaminari

I am currently using Kaminari for pagination, and I am aware of the link_to_next_page method. 我目前正在使用Kaminari进行分页,并且我知道link_to_next_page方法。 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 表单通过:remote => true提交
  3. Javascript programmatically directs the user to the next page. Javascript以编程方式将用户定向到下一页。

Sort of like: create.js.erb 有点像: 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? 我可以生成/确定下一页是什么,然后通过javascript重定向用户吗?

Thanks! 谢谢!

Final solution: Based on Kieran's help below, here's what I ended up doing: 最终解决方案:基于下面的Kieran帮助,这是我最终要做的事情:

<% 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. 这似乎对我来说很好。

The problem you have is that Kaminari requires the "scope" in order to correctly ascertain the next page from your current page: 您遇到的问题是Kaminari需要“范围”才能正确确定当前页面的下一页:

https://github.com/amatsuda/kaminari/blob/bc51fd08f9e67b8338b421f6827d7d8e54c82deb/lib/kaminari/helpers/action_view_extension.rb#L64 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. 由于您要提交到javascript,因此需要为此方法提供此范围以使其起作用。 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. 在控制器的create方法中,您将需要具有相同的代码来查询要在其上进行分页的对象,然后让Kaminari进行其余操作。

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

You will need to ensure that the form also sends along the params[:page] 您将需要确保表单也发送了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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM