Joe Essey
Joe Essey

Reputation: 3527

How to build specific resource routes in routes.rb

I have an app where I want to never be able to delete a Foo. How can I configure resources :foos to not create the delete route?

Also, foos belong to bars and should only be shown on the bars show page. I've tried the following get routes but I get the associated errors:

get "/foos/:id"

ArgumentError at /bars/1220
missing :controller

--

get "/foos"

ArgumentError at /bars/1220
missing :action

--

get "foos/show"

No route matches [GET] "/bars/1220" 

(There is most definitely a route for this, and it works if I leave resources :foos in the routes.rb file.)

I've read routing from the outside in a couple times but I guess I'm not getting it. Any help would be appreciated.

Upvotes: 0

Views: 141

Answers (2)

amb110395
amb110395

Reputation: 1545

To prevent a delete route from being created do this:

resources :foos, :except => :destroy

With respect to your second question (if I understand it correctly), you have to use nested resources:

resources :bars do
  resources :foos, :except => :destroy
end

This will create many routes including:

/bars/:bar_id/foos/:id

Upvotes: 1

MrTheWalrus
MrTheWalrus

Reputation: 9700

You can specify which of the standard routes to include when you use the resources command, with the except and only options:

resources :foo, :except => [:destroy] do
end

or

resources :foo, :only => [:index, :create, :show] do
end

Upvotes: 2

Related Questions