1

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

2 Answers 2

4

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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.