Ruby-on-rails :as in rails routes.rb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4696229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
:as in rails routes.rb
提问by ryanprayogo
In config/routes.rb, I tried both:
在config/routes.rb,我尝试了两个:
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.
当我点击 时http://localhost:3000/,两种方法都有效,似乎没有什么不同。
What is the :asoption used for?
:as选项有什么用?
回答by Andy Lindeman
The :as option forms a named route.
:as 选项形成一个命名路由。
Usually it's used in a non-root route. For example:
通常它用于非根路由。例如:
match '/search' => 'search#search', :as => 'search' # SearchController#search
You could then do something like:
然后,您可以执行以下操作:
<%= link_to search_path, 'Click Here to Search!' %>
search_pathand search_urlare defined because of the :as
search_path并且search_url被定义为:as
For a root route, you don't really need :asbecause the the URL helpers root_pathand root_urlare defined for you by Rails.
对于根路由,您实际上并不需要,:as因为 URL 帮助器root_path和root_url由 Rails 为您定义。
回答by Roman Bambycha
Rails 4 compatible.
Rails 4 兼容。
In path_to_your_app/config/routes.rb
在 path_to_your_app/config/routes.rb
get "/profile/edit" => "users#profile_edit", :as => "edit_me"
Since ruby 2.0 you can use:
从 ruby 2.0 开始,您可以使用:
get "/profile/edit", to: "users#profile_edit", as: "edit_me"
In path_to_your_app/app/views/**inrequired view
在path_to_your_app/app/views/**in所需视图中
<%= link_to "Edit profile", edit_me_path %>
Do not use matchif you aren't sure you need it:
match如果您不确定是否需要,请不要使用:
It creates a vulnerability when you use it in next pattern:
当您在下一个模式中使用它时,它会创建一个漏洞:
match ':controller/:action/:id'
From documentation:
从文档:
You should not use the
matchmethod in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add via:[:get, :post]option. If you want to expose your action to GET, use get in the router:Instead of:
match "controller#action"Do:
get "controller#action"
您不应
match在未指定 HTTP 方法的情况下在路由器中使用该方法。如果您想同时向 GET 和 POST 公开您的操作,请添加 via:[:get, :post]选项。如果您想将您的操作公开给 GET,请在路由器中使用 get:代替:
match "controller#action"做:
get "controller#action"
Read more about:
阅读更多关于:
About match
关于比赛
http://github.com/rails/rails/issues/5964
http://github.com/rails/rails/issues/5964
About routes mapping
关于路由映射
http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/Mapper/Base/match
http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/Mapper/Base/match
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html
About routes in general
关于一般路线
http://api.rubyonrails.org/classes/ActionDispatch/Routing.html
http://api.rubyonrails.org/classes/ActionDispatch/Routing.html
回答by David Sulc
The :asoption 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 named root), but is very useful for new routes you add.
该:as选项创建一个命名路径。然后,您可以在控制器和视图中调用此路径(例如redirect_to things_path)。这对于根路径不是很有用(因为它已经命名为root),但是对于您添加的新路由非常有用。

