Jeroen janssen
Jeroen janssen

Reputation: 11

How to add a new custom resource to routes Rails 3

How do I add a custom route for a new resource in the Rails 3 routes?

I know how to do it for collections and members but this style doesn't seem to be working for new resources. Is this a bug or am I doing something wrong?

So these work:

collection do
  get :wish
end

member do
  get :wish
end

But this doesn't work:

new do
  get :wish
end

Upvotes: 1

Views: 3051

Answers (2)

sameera207
sameera207

Reputation: 16629

Try this:

resources :<resource name> do
  member do
    get '<custom action>'
  end
end

As an example lets see you have a controller called 'main' and if you have a custom action 'dashbord'

resources :admin do
  member do
    get 'dashbord'
  end
end

Upvotes: 4

Andrew
Andrew

Reputation: 43153

In other words you want to match to something like:

example.com/foos/new/custom rather than example.com/foos/1/custom/ or example.com/foos/custom

That's not RESTful, which just means there isn't an automatic route for it. You should be able to do it using non-resourceful routing, ie something like this should work:

match 'resource/new/custom'=>'resource#custom'

... where 'custom' is the action name in your controller.

See the rails guide for more options and details.

Upvotes: 1

Related Questions