0

这是我的link_to

<%= link_to sub_category.name, controller: :posts, action: :product, id: "#{sub_category.slug}-#{sub_category.id}" %>

哪个指向 url

http://localhost:3000/posts/product/fifith-category-s-sub-category-2

我需要如下网址

http://localhost:3000/fifith-category-s-sub-category-2

我该怎么做。

我的路线.rb

resources :posts
match ':controller(/:action(/:id))(.:format)', via: [:get,:post]
4

2 回答 2

0

@MarekLipka 的建议是正确的,但是像这样定义您的路由将占用您应用程序中的所有单级命名空间,即“/anything”将默认路由到posts#product.

我建议使用某种形式的标识符来确定应该去哪些路线posts#product。什么对你有用取决于你为什么要这样做。几个选项是:

使用短命名空间:

scope '/pp' do 
    get ':id', to: 'posts#product
end
# "/pp/:id" routes to 'posts/product'
# pp is a random short name I picked, it could be anything

# link
<%= link_to sub_category.name, "pp/#{sub_category.slug}-#{sub_category.id}" %> 

使用约束:

get ':id', to: 'posts#product`, constraints: { :id => /sub\-category/ }
# only id's with 'sub-cateogry' route to 'posts/product'

# link (assuming that sub_category.slug has 'sub-category' words in it)
<%= link_to sub_category.name, "#{sub_category.slug}-#{sub_category.id}" %>  
于 2013-10-14T07:58:28.843 回答
0

如果你希望路径/:id匹配你的posts#product,你应该在你的路线中有这样的东西:

resources :posts
match ':id', to: 'posts#product`, via: :get
match ':controller(/:action(/:id))(.:format)', via: [:get, :post]
于 2013-10-14T07:32:06.797 回答