Undistraction
Undistraction

Reputation: 43401

Adding a second 'new' action to a resource's routes

I need to add a second 'new' action (called *different_new*) to a resource called Things. I would like it to resolve to:

things/different_new

I have a different_new action defined on my *things_controller.rb*

Following the rails guide to routing I see this example for adding a custom action as a route:

resources :photos do
  member do
    get 'preview'
  end
end

However any action added like this is added to a specific resource. For example the above would result in a route like this:

photos/:photo_id/preview

SO this will not work for me as I want the action to be where a new resource is created, not an action for a resource that is already created.

How can I solve this?

Upvotes: 0

Views: 176

Answers (2)

Jason Noble
Jason Noble

Reputation: 3766

Similar to member, there is also a collection option:

resources :photos do
  collection do
    get 'preview'
  end
end

Or for your case:

resources :things do
  collection do
    get 'different_new'
  end
end

This is described in section 2.9.2 on the rails routing guide.

Upvotes: 3

KL-7
KL-7

Reputation: 47678

New action (in this case your different_new action) is not associated with any existing resource, so it should be a collection route:

resources :things do
  get :different_new, :on => :collection
end

It'll generate a path like things/different_new. Though, unlike with predefined new action url helper will be plural - different_new_things_path.

Upvotes: 2

Related Questions