Customising resourceful routes in Rails 3

I wonder is anyone can give me a pointer in the right direction.

we've recently upgraded our app to Rails 3. We previous had a
situation where it was necessary to allow our create action on a
resourceful controller to be accessible in the following way

GET /resources/create -> ResourcesController#create
POST /resources -> ResourcesController

This would do the job
resources :resources, :collection { :create => :get, :authorize => :get }

and we'd get the following router helper to boot

create_resources_path/url

In Rails 3 this has changed however and now I'm finding it extremely
difficult to replicate

resources :resources do
  collection do
    get :create
    get :authorize
  end
end

This would be the most obvious way I would think to solve this.

but alas this route doesn't exist

create_resources GET /resources/create -> ResourcesController#create

however this does, so the create action is being treated differently
and so needs some other modification to make it work as I would like
it to.

authorize_resources GET /resources/authorize -> ResourcesController#authorize

The following does part of the job, but renders

resources :resources do
  collection do
    get :create, :as => 'create'
    get :authorize
  end
end

create_resources GET /resources -> ResourcesController#create

This of course then overrides the index route for this controller

resources GET /resources -> ResourcesController#index

This also seems to have no effect?!?

resources :resources, :path_names => {:create => 'create' } do
  collection do
    get :create, :as => 'create'
    get :authorize
  end
end

Can anyone help?

You’re having a hard time with “create” because it’s one of the default actions (show, create, update, destroy, new, edit). There’s some hard coding in Rails that’s interfering with what you’re trying to do. In this case, if the action is “create”, it excludes the action from the path.

Overriding the default actions generally isn’t a good idea. That being said, you can force rails to do what you want by forcing the path option:

get :create, :as => :create, :path => :create

Thanks Tim, that's exactly what I was looking for.

I don't disagree that messing too much with these defaults is
something I'd want to do often and without reason. Effectively this is
just another route to an existing action, in the same way that get
:authorize. Had this not worked so easily in Rails 2 I might have
opted for resources/generate and this wouldn't have been an issue :slight_smile:

Cheers

RobL