In config/routes.rb
, I tried both:
root :to => 'things#index', :as => 'things'
and
root :to => 'things#index'
When I hit http://localhost:3000/
, both approaches work, and nothing seems to be different.
What is the :as
option used for?
In config/routes.rb
, I tried both:
root :to => 'things#index', :as => 'things'
and
root :to => 'things#index'
When I hit http://localhost:3000/
, both approaches work, and nothing seems to be different.
What is the :as
option used for?
The
:as
option creates a named path. You can then call this path in your controllers and views (e.g.redirect_to things_path
). This isn't very useful for the root path (as it already namedroot
), but is very useful for new routes you add.The :as option forms a named route.
Usually it's used in a non-root route. For example:
You could then do something like:
search_path
andsearch_url
are defined because of the:as
For a root route, you don't really need
:as
because the the URL helpersroot_path
androot_url
are defined for you by Rails.Rails 4 compatible.
In
path_to_your_app/config/routes.rb
Since ruby 2.0 you can use:
In
path_to_your_app/app/views/**in
required viewDo not use
match
if you aren't sure you need it:It creates a vulnerability when you use it in next pattern:
From documentation:
Read more about:
About match
http://github.com/rails/rails/issues/5964
About routes mapping
http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/Mapper/Base/match
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html
About routes in general
http://api.rubyonrails.org/classes/ActionDispatch/Routing.html