14

In my rails app following in routes.rb

resources :users

leads to following output for 'rake routes'

 users        GET    /users(.:format)                 users#index
              POST   /users(.:format)                 users#create
 new_user     GET    /users/new(.:format)             users#new
 edit_user    GET    /users/:id/edit(.:format)        users#edit
 user         GET    /users/:id(.:format)             users#show
              PUT    /users/:id(.:format)             users#update
              DELETE /users/:id(.:format)             users#destroy

& following in routes.rb (for my custom controller 'home')

match  '/new_user'        =>          'home#new_user', via: [:get]
match  '/users/:id/edit'  =>          'home#edit_user', via: [:get]
match  '/users/:id'       =>          'home#show_user', via: [:get]
match  '/users/:id'       =>          'home#create_user', via: [:post]

leads to following output for 'rake routes'

GET    /new_user(.:format)                home#new_user
GET    /users/:id/edit(.:format)          home#edit_user
GET    /users/:id(.:format)               home#show_user
POST   /users/:id(.:format)               home#create_user

why there are no path names for second case? like in first case ('new_user', 'edit_user')

is there any way to have path names for second case? as i want to use these path names in my views

Akhil
  • 6,495
  • 3
  • 27
  • 59

1 Answers1

40

There are no path names because you haven't specified path names. If you're supplying custom routes instead of using resources, you need to use :as to provide a pathname:

match '/new_user' => 'home#new_user', via: :get, as: :new_user

You should also just use get instead of match... via: :get:

get '/new_user' => 'home#new_user', as: :new_user

However, given your set of routes, your better bet is to continue using resources, but to supply a limited list of actions via :only and a custom controller via :controller:

resources :users, only: %w(new edit show create), controller: "home"
meager
  • 209,754
  • 38
  • 307
  • 315
  • 2
    Thanks.. that was quick, +1 :) , one more question is there any advantage of dropping 'match... via: :get'? as you said above – Akhil Apr 08 '13 at 18:27
  • 2
    My way is shorter and clearer. The better question is: Is there any advantage to using `match... via: :get` over `get`? – meager Apr 08 '13 at 18:31
  • 1
    got it..i will go your way – Akhil Apr 08 '13 at 18:33