开发者

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 RESTf开发者_高级运维ul, how do i implements REFTful routes for it?

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/

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

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


I had the same problem. I wrote my own LinkRenderer like this to fully use nested routes.

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 :params => { :url => project_task_lists_path(@project) }, :renderer => PaginationListLinkRenderer %>

I hope this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜