方法
- U
实例公共方法
url_for(options = nil) 链接
返回给定options的URL。这接受与Action Controller中的url_for相同的选项(请参阅ActionDispatch::Routing::UrlFor#url_for的文档)。请注意,默认情况下:only_path为true,因此您将获得相对的"/controller/action"而不是像"http://example.com/controller/action"这样的完整限定URL。
选项¶ ↑
-
:anchor- 指定要附加到路径的锚点名称。 -
:only_path- 如果为true,则返回相对URL(省略协议、主机名和端口)(默认情况下为true,除非指定了:host)。 -
:trailing_slash- 如果为true,则添加一个尾部斜杠,如"/archive/2005/"。请注意,这目前不被推荐,因为它会破坏缓存。 -
:host- 如果提供了:host,则覆盖默认的(当前)主机。 -
:protocol- 如果提供了:protocol,则覆盖默认的(当前)协议。 -
:user- 内联HTTP认证(仅在同时存在:password时才会提取)。 -
:password- 内联HTTP认证(仅在同时存在:user时才会提取)。
依赖命名路由¶ ↑
将记录(如Active Record)而不是哈希作为options参数传递将触发该记录的命名路由。查找将基于类的名称。因此,传递一个Workshop对象将尝试使用workshop_path路由。如果您有嵌套路由,例如admin_workshop_path,您必须显式调用它(url_for无法猜测该路由)。
隐式控制器命名空间¶ ↑
使用:controller选项传入的控制器将保留其命名空间,除非它是绝对命名空间。
示例¶ ↑
<%= url_for(action: 'index') %>
# => /blogs/
<%= url_for(action: 'find', controller: 'books') %>
# => /books/find
<%= url_for(action: 'login', controller: 'members', only_path: false, protocol: 'https') %>
# => https://www.example.com/members/login/
<%= url_for(action: 'play', anchor: 'player') %>
# => /messages/play/#player
<%= url_for(action: 'jump', anchor: 'tax&ship') %>
# => /testing/jump/#tax&ship
<%= url_for(Workshop) %>
# => /workshops
<%= url_for(Workshop.new) %>
# relies on Workshop answering a persisted? call (and in this case returning false)
# => /workshops
<%= url_for(@workshop) %>
# calls @workshop.to_param which by default returns the id
# => /workshops/5
# to_param can be re-defined in a model to provide different URL names:
# => /workshops/1-workshop-name
<%= url_for("http://www.example.com") %>
# => http://www.example.com
<%= url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com
<%= url_for(:back) %>
# if request.env["HTTP_REFERER"] is not set or is blank
# => javascript:history.back()
<%= url_for(action: 'index', controller: 'users') %>
# Assuming an "admin" namespace
# => /admin/users
<%= url_for(action: 'index', controller: '/users') %>
# Specify absolute path with beginning slash
# => /users
# File actionview/lib/action_view/routing_url_for.rb, line 82 def url_for(options = nil) case options when String options when nil super(only_path: _generate_paths_by_default) when Hash options = options.symbolize_keys ensure_only_path_option(options) super(options) when ActionController::Parameters ensure_only_path_option(options) super(options) when :back _back_url when Array components = options.dup options = components.extract_options! ensure_only_path_option(options) if options[:only_path] polymorphic_path(components, options) else polymorphic_url(components, options) end else method = _generate_paths_by_default ? :path : :url builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.public_send(method) case options when Symbol builder.handle_string_call(self, options) when Class builder.handle_class_call(self, options) else builder.handle_model_call(self, options) end end end