我是铁道新手。在我的演示应用程序中,我可以在http://localhost:3000/products
中显示我的所有产品列表,每个产品都有显示,编辑,删除选项。当我点击显示链接时,我会转到特定产品,我会得到该产品的网址
http://localhost:3000/products/5
(在/ products /之后是id)但我想要添加两件事
1)当我点击“显示”按钮时,我的网址应该是这样的
http://localhost:3000/products/display
ID不应该是产品之后。只显示。
2)我想添加另一个按钮,我可以在其中显示所有产品列表中的所有产品列表,例如http://localhost:3000/products/displayall
,我的所有产品都会在http://localhost:3000/products
和http://localhost:3000/products/displayall
中显示
答案 0 :(得分:0)
您已经引用了显示,编辑和删除操作,因此您可能会定义资源丰富的路线。如果这是正确的,您可以将自定义路由添加到Product
路由via a collection
block:
# config/routes.rb
resources :products do
collection do
get 'display'
get 'displayall'
end
end
这会添加以下两组命名路由:
display_products GET /products/display(.:format) products#display
displayall_products GET /products/displayall(.:format) products#displayall
这些路由分别指向products#display
和products#displayall
控制器操作。
答案 1 :(得分:0)
您可以使用它来覆盖默认的CRuD行为
resource :products do
get ':id/show', :to => :show
get ':id/display', :to => :show
get 'displayall', :to => :index, :on => :collection
end
http://guides.rubyonrails.org/routing.html
如下面2.10.1所述的链接,使用:on => :成员添加一个能够识别/products/:id/display
的宁静链接,否则就是下面的例子。