3

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

   # 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

为了测试,如果我只是在 url 栏中使用页面参数输入正确的路径

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

它工作正常并分页,显示下一组产品。我的目标是让链接dashboard中有链接。我猜这将通过视图中“will_paginate”方法中的某些传递选项组合发生。

我希望当用户位于其根页面时发生这种情况:

  http://localhost:3000/dashboard
4

2 回答 2

5

要覆盖 will_paginate url:

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

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

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

将分页文档

于 2013-09-18T03:58:56.370 回答
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'
于 2020-06-23T02:37:47.683 回答