Ruby on Rails Routing
TRACK

Ruby on Rails Routing

Avatar

Ruby on Rails routing can be a little confusing. Here are some of the high points.

The root path is specified by the 'root' method. It matches the '/' path and behaves like 'match'.

The 'match' method will match the requested path to the string passed to match and route to the controller and action specified.

# Sends requests to /users/list to PeopleController#index
match "users/list", :to => "people#index"

You can also extract variables from the path.

# Matches /user/1/edit and passes '1' in params[:id]
match "users/:id/edit", :to => "users#edit"

You can specify which HTTP request methods are valid for a route.

get "users/index"

match "users/index", :to => "users#index", :via => :get

You can also name a route and use the related helper methods.

match "users/index", :to => "users#index", :as => "user_index"

#In some controller or view
user_index_path
# => "/users/index"

user_index_url
# => "http://localhost:3000/users/index"

You can also create RESTful resources a

Comments

Avatar