Modify URL Pattern Ruby Rails

旧时模样 提交于 2019-12-12 16:00:25

问题


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.

I have done the following:

ProductsController

def showall
  @products = current_user.products
end

routes

resources :products do
  get :showall
end

I am aware that because of the nested resources the URL Pattern became

 /products/:product_id/showall(.:format)

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.


回答1:


You should change your route definition as follows:

resources :products do
  collection do 
    get :showall
  end
end

Check corresponding documentation.

Hope that helps!



来源:https://stackoverflow.com/questions/29652146/modify-url-pattern-ruby-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!