:就像在rails routes.rb中一样 [英] :as in rails routes.rb

查看:30
本文介绍了:就像在rails routes.rb中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

config/routes.rb 中,我都尝试了:

In config/routes.rb, I tried both:

root :to => 'things#index', :as => 'things'

root :to => 'things#index'

当我点击 http://localhost:3000/ 时,两种方法都有效,似乎没有什么不同.

When I hit http://localhost:3000/, both approaches work, and nothing seems to be different.

:as 选项有什么用?

推荐答案

:as 选项形成一个命名路由.

The :as option forms a named route.

通常用于非根路由.例如:

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_pathsearch_url 是因为 :as

对于根路由,您实际上并不需要 :as,因为 URL 帮助程序 root_pathroot_url 是为您定义的导轨.

For a root route, you don't really need :as because the the URL helpers root_path and root_url are defined for you by Rails.

这篇关于:就像在rails routes.rb中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆