The problem with will_paginate page links

I currently have a comment model that is placed under the micropost, and both of them appear on the same page. The problem is that both of them are displayed on the same page, and both of them are paginated, and I'm trying to move on to facebook's micro-posting approach. Here is the question below:

Links for both pagination turn into href="/users/2?page=2" , not href="/users/2/micropost?page=2" or href="/users/2/comment?page=2" . I am not sure how to solve this problem. Here are some of my code. All suggestions are welcome!

Micropost Render HTML

 <table class="microposts"> <% if microposts.any? %> <%= render microposts %> <%= will_paginate microposts, :page_links => false %> <% else %> <div class="EmptyContainer"><span class='Empty'>Add a thread!</span></div> <% end %> </table> 

HTML Comment Section

 <div id='CommentContainer-<%= micropost.id%>' class='CommentContainer Condensed2'> <div class='Comment'> <%= render :partial => "comments/form", :locals => { :micropost => micropost } %> </div> <div id='comments'> <% comments = micropost.comments.paginate(:per_page => 5, :page => params[:page]) %> <%= render comments %> <%= will_paginate comments, :class =>"pagination" %> </div> </div> 

User controller for browse page

  def show @user = User.find(params[:id]) @comment = Comment.find(params[:id]) @micropost = Micropost.new @comment = Comment.new @comment = @micropost.comments.build(params[:comment]) @comments = @micropost.comments.paginate(:page => params[:page], :per_page => 5) @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 10, :page => params[:page]) respond_to do |format| format.html format.js end end 
+6
source share
1 answer

The problem lies within the will_paginate way of creating URLs for each page (this has nothing to do with jQuery).

By design, will_paginate will try its best to guess what the base url for the page user is tied to (internally, it uses the controller / action for this). This base url is then combined with any additional parameters passed to the will_paginate helper using :params and successful page numbers.

Now (will_paginate 3.0.3), to overwrite this default behavior, you need to write your own LinkRenderer class. The following is an example of such a class - it uses a new, additional option :base_link_url , which can be passed to the will_paginate help assistant. The skipped string is then used as the base for linking to pages. If the parameter :base_link_url not passed, it will overwrite the default behavior.

Place the following class somewhere where the rails can find it at boot (for example, lib if you added / lib in the startup path in application.rb ):

 # custom_link_renderer.rb class CustomLinkRenderer < WillPaginate::ActionView::LinkRenderer def prepare(collection, options, template) @base_link_url = options.delete :base_link_url @base_link_url_has_qs = @base_link_url.index('?') != nil if @base_link_url super end protected def url(page) if @base_link_url.blank? super else @base_url_params ||= begin merge_optional_params(default_url_params) end url_params = @base_url_params.dup add_current_page_param(url_params, page) query_s = [] url_params.each_pair {|key,val| query_s.push("#{key}=#{val}")} if query_s.size > 0 @base_link_url+(@base_link_url_has_qs ? '&' : '?')+query_s.join('&') else @base_link_url end end end end 

Using:

 # in your view will_paginate collection, :renderer => CustomLinkRenderer, :base_link_url => '/anything/you/want' 

Now back to your case. By this time, you will probably see a solution - you can have two will_paginate widgets on the same page with different base URLs, passing different :base_link_url parameters for these two.

+8
source

Source: https://habr.com/ru/post/909976/


All Articles