如何创建will_paginate链接到其他页面?

时间:2013-09-18 01:39:16

标签: ruby-on-rails routing will-paginate

我正在尝试为产品做分页。我有:

   # Routes
    match 'dashboard' => 'dashboard#index'

   # Dashboard Controller
   def index 
    @products = Product.order("id").page(params[:page]).per_page(4)
   end

   # Dashboard Index View
   h1 products
   .products
   = render @products
   = will_paginate(@products)

分页链接显示在仪表板索引视图中。

实施例

     <-- previous 1 2 3 next -->

但是链接没有附加正确的路径。默认视图为localhost:3000 / dashboard 分页链接有:

     http://localhost:3000/?page=2

进行测试,如果我只是在网址栏中输入页面参数的正确路径

     http://localhost:3000/dashboard?page=2

它工作正常并分页,显示下一组产品。我的目标是让链接中包含dashboard个链接。我猜这种情况会发生在视图中“will_paginate”方法中的一些传递选项的组合中。

我希望当用户在他们的根页面时发生这种情况:

  http://localhost:3000/dashboard

2 个答案:

答案 0 :(得分:3)

覆盖will_paginate网址:

will_paginate(@products, :params => { :controller => "dashboard", :action => "index" })

或者,如果您为仪表板命名了路线:

will_paginate(@products, :params => { :controller => dashboard_path })

will paginate documentation

答案 1 :(得分:0)

对于仍然有此问题的任何人:

# View
# Generating pagination links for collection in another controller/action.
will_paginate @products, params: { controller: 'dashboard', action: 'index' } 

和:

# routes.rb
get 'dashboard', to: 'dashboard#index' # Before root, 2 route to the same action, Rails/will_paginate will use the first matched one in routes.rb.
root 'dashboard#index'