Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify URL Pattern Ruby Rails

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.

like image 906
ikanyu Avatar asked May 20 '26 18:05

ikanyu


1 Answers

You should change your route definition as follows:

resources :products do
  collection do 
    get :showall
  end
end

Check corresponding documentation.

Hope that helps!

like image 172
Paweł Dawczak Avatar answered May 23 '26 11:05

Paweł Dawczak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!