Ruby on Rails の page_cache の will_paginate のリンク形式を変更するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2045694

質問

will_paginate で page_cache を使用したいと考えています。

以下のページに良い情報があります。

http://railsenvy.com/2007/2/28/rails-caching-tutorial#pagination http://railslab.newrelic.com/2009/02/05/episode-5-advanced-page-caching

Routes.rb は次のように書きました。

map.connect '/products/page/:page', :controller => 'products', :action => 'index'

ただし、URL のリンクは will_paginate ヘルパー内の '/products/page/:page' には変更されません。これらはまだ「products?page=2」です。

will_paginate の URL 形式を変更するにはどうすればよいですか?

役に立ちましたか?

解決

の上で宣言したそのルートは、任意のRESTfulなリソースの路線ですか?それは、あなたのルートファイルは、以下のようになりますされます:

map.connnect '/products/page/:page', :controller => 'products', :action => 'index'
map.resources :products, :except => [:index]
あなたのルートが正しい見れば、

、あなたは猿パッチングwill_paginateページへのリンクを生成する方法を試みることができます。それはWillPaginate::ViewHelpers#url_for(page)にそう。これは、いくつかのトリッキーなエッジケースを処理するために、いくつかのかなり複雑なロジックだが、あなたは最初にあなたのproductsためのシンプルなバージョンを試してみました、新しいバージョンを書くことができます:

# in lib/cache_paginated_projects.rb
WillPaginate::ViewHelpers.class_eval do
  old_url_for = method(:url_for)
  define_method(:url_for) do |page|
    if @template.params[:controller].to_s == 'products' && @template.params[:action].to_s == 'index'
      @template.url_for :page => page
    else
      old_url_for.bind(self).call(page)
    end
  end
end

他のヒント

これは私のために動作します。

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)}/page/#{page}", attributes
  end
end

ファイルをconfig/environment.rbするために、この行を追加します。

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

現在の答えに少し追加すると、それを理解するのに何時間も費やす必要がありました。

たとえば私の場合はフィルタリングを含めるなど、より複雑なルートがある場合は、 「より高いレベル」のルートが最初に来るようにしてください (RESTful なものより上にあるというだけではありません)、そうでない場合、will_paginate は最初に使用可能なものを選択し、あまり美しくない方法で URL の末尾に余分なパラメータを貼り付けます。

したがって、私の場合は次のようになりました。

map.connect "wallpapers/:filter/page/:page", :controller => "wallpapers", :action => "index", :requirements => {:page => /\d+/, :filter => /(popular|featured)/ }
map.connect "wallpapers/page/:page", :controller => "wallpapers", :action => "index", :requirements => {:page => /\d+/ }
map.resources :wallpapers

これで、次のようなきれいな URL が得られます。 壁紙/人気/ページ/2 の代わりに 壁紙/ページ/2?フィルター=人気

これを行います:

map.products_with_pages "/products/page/:page", :controller => "products", :action => "index"

あなたも、has_manyの、すなわちでそれを行うことができます:製品has_manyの:アイテム

map.resources :products do |product|
  map.items_with_pages "/products/:id/page/:page", :controller => "products", :action => "show"
end

次に、あなたのコントローラは、

のようになります。
def show
  product = Product.find(params[:id])
  @items = product.items.paginate :per_page => 5, :page => params[:page]
end

のようにあなたのURLを与えることになる: http://domain.com/products/123 123は製品IDであり、図3は、ページIDである/ページ/ 3 に。また、パーマリンクを使用し、123 idは、よりSEOフレンドリーな単語に変更されている可能性があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top