本文介绍了修改URL模式Ruby Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试显示一个页面,以显示当前用户的所有产品.因此,我在产品视图下创建了一个新页面showall.html.erb.
I am trying to display a page to show all products by current user. Because of this, I have created a new page showall.html.erb under products view.
我已经执行以下操作:
ProductsController
ProductsController
def showall
@products = current_user.products
end
路线
resources :products do
get :showall
end
我知道,由于资源的嵌套,URL模式变成了
I am aware that because of the nested resources the URL Pattern became
/products/:product_id/showall(.:format)
我实际上如何摆脱product_id部分,以实现/products/showall拥有一个特殊页面来呈现当前用户的所有产品.
How do I actually get rid of the product_id part to achieve /products/showall to have one special page to render all products by the current user.
推荐答案
您应如下更改路由定义:
You should change your route definition as follows:
resources :products do
collection do
get :showall
end
end
检查相应的文档.
希望有帮助!
这篇关于修改URL模式Ruby Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!