LeroyJ
LeroyJ

Reputation: 31

Renaming path helpers in Rails 3 routing

I have a projects controller/model. Instead of listing projects on the #index page, I show a list of drop downs, which submits to projects#select, which finds the right Project (I've made sure there can only be 1 for each combination of options) and forwards the user to the #show page for that Project.

So for my routes I do this...

resources :projects, :only => [:index, :show] do
  collection do
    get 'select'
  end
end

And thats fine, but the helper method for #select is 'select_projects', which is understandable but in my case I really want 'select_project'. And I really don't want to alias this in another file. No problem I can use :as...

resources :projects, :only => [:index, :show] do
  collection do
    get 'select', :as => 'select_project'
  end
end

But now my helper is 'select_project_projects'. So I cheat a little (still better than aliasing in another file)...

resources :projects, :only => [:index, :show]
match '/projects/select', :to => 'projects#select', :as => 'select_project'

This looks like it might work, but it doesn't because /project/select actually matches the route for 'project#show'. Changing the order of the lines does the trick.

match '/projects/select', :to => 'projects#select', :as => 'select_project'
resources :projects, :only => [:index, :show]

But is there a more elegant way of handling this? I realize this is borderline OCD, but I'd like to be able to have complete control over the route name within the resources block.

Upvotes: 3

Views: 1871

Answers (3)

Fellow Stranger
Fellow Stranger

Reputation: 34013

For those that want to rename the helper method side of things (as the title suggests):

resources :posts, as: "articles"

Upvotes: 0

Zakwan
Zakwan

Reputation: 1072

use resource instead of resources

Upvotes: 2

Damien
Damien

Reputation: 21

you probably don't want to make it a collection route but a member route:

resources :projects, :only => [:index, :show] do
  member do
    get 'select'
  end
end

This way you'll have the select_project helper.

Upvotes: 1

Related Questions