简体   繁体   English

带分页的rails动态路由

[英]rails dynamic route with pagination

I have the following routes:我有以下路线:

Brand.all.each do |brand|
  get "/#{brand.slug}", to: 'brands#index'#
end

That generates:这会产生:

"/tommy"
"/lacoste"
"/gap"

But when I access /lacoste for example, the page has a pagination (kaminari or will-paginate).但是,例如,当我访问 /lacoste 时,该页面有一个分页(kaminari 或 will-paginate)。 The pagination links are generated like this:分页链接生成如下:

"/tommy?page=2

The paginate method always shows the first brand route generated. paginate 方法始终显示生成的第一个品牌路线。

How can I solve this?我该如何解决这个问题?

I suggest the following approach我建议以下方法

Add this route at the end of the routes.rb .routes.rb的末尾添加这条路线。

get "/:slug", to: 'brands#index'

And you can then find the brand based on the params[:slug] value.然后您可以根据params[:slug]值找到品牌。

def index
  @brand = Brand.find_by(name: params[:slug])
  # extra code 
end

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

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