Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why no path names for custom routes in Rails

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

like image 995
Akhil Avatar asked Apr 08 '13 18:04

Akhil


People also ask

How do I find a specific route in Rails?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

What is namespace in Rails routes?

In a way, namespace is a shorthand for writing a scope with all three options being set to the same value.

How do routes work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.


1 Answers

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"
like image 200
meagar Avatar answered Oct 17 '22 02:10

meagar