raphael_turtle
raphael_turtle

Reputation: 7314

Remove id from link in rails route

I want to have "cart/:id" link to "cart", this works ok when typing in the url 'store/cart' but when I use a link it always includes the id

= link_to 'view cart', cart_path(@cart)

results in the url 'store/cart/1'

my routes.rb

 scope 'store' do
     resources :carts, only: [:destroy]
     match 'cart', to: 'carts#show', via: :get
  end

Upvotes: 3

Views: 3017

Answers (1)

denis-bu
denis-bu

Reputation: 3446

Use resource method.

Try this way:

scope 'store' do
  resource :cart, only: [:show, :destroy]
end

Here's API docs

UPDATE

Also, avoid passing @cart to cart_path. Controller should figure out how to get resource instance without id in url.

Common way to get singular resource instance is to store database id in session or if you have some user authentication framework - link it to current user (most frameworks will ultimately puts user id in session).

Upvotes: 6

Related Questions