简体   繁体   English

使用will_paginate的RESTful分页路由

[英]RESTful pagination routes using will_paginate

I have nested routes like this : 我有这样的嵌套路线:

map.resources :foo do |foo|
  foo.resources :bar do |bar|
    bar.resources :baz
  end
end

i have list with pagination in the index action for each resource, i need to caches each of this pages, to do so i need the routes to be RESTful, how do i implements REFTful routes for it? 我在每个资源的索引操作中都有分页列表,我需要缓存每个页面,为此我需要路由是RESTful,我如何为它实现REFTful路由?

for example i want the route will be like this : 例如,我希望路线将是这样的:

http://www.example.com/foo/:id/pages/:page_number
http://www.example.com/foo/:id/bar/:id/pages/:page_number

create custom_link_renderer.rb in app/helpers/ 在app / helpers /中创建custom_link_renderer.rb

class CustomLinkRenderer < WillPaginate::LinkRenderer
  def page_link(page, text, attributes = {})
    @template.link_to text, "#{@template.url_for(@url_params)}/pages/#{page}", attributes
  end
end

add this line to config/environment.rb 将此行添加到config/environment.rb

WillPaginate::ViewHelpers.pagination_options[:renderer] = 'CustomLinkRenderer'

I had the same problem. 我有同样的问题。 I wrote my own LinkRenderer like this to fully use nested routes. 我这样编写了自己的LinkRenderer来充分利用嵌套路由。

class PaginationListLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer


protected

  def page_number(page)
    unless page == current_page
      if !@options[:params][:url].to_s.empty?
        tag(:li, link(page, @options[:params][:url] + "?page=" + page.to_s))
      else
        tag(:li, link(page,  page, :rel => rel_value(page)))
      end
    else
      tag(:li, page, :class => "current")
    end
  end

  def previous_or_next_page(page, text, classname)
    if page
      if !@options[:params][:url].to_s.empty?
        tag(:li, link(text, @options[:params][:url] + "?page=" + page.to_s, :class => classname))
      else
        tag(:li, link(text,  page, :rel => rel_value(page), :class => classname))
      end
      #tag(:li, link(text, page), :class => classname)
    else
      tag(:li, text, :class => classname + ' disabled')
    end
  end

  def html_container(html)
    tag(:ul, html, container_attributes)
  end

end

Then you have to call will_paginate with this parameters: 然后你必须用这个参数调用will_paginate:

<%= will_paginate :params => { :url => project_task_lists_path(@project) }, :renderer => PaginationListLinkRenderer %>

I hope this helps :) 我希望这有帮助 :)

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

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