Action Dispatch Routing UrlFor¶ ↑
在 config/routes.rb 中,您定义了 URL 到控制器的映射,但反向操作也是可能的:可以根据路由定义生成 URL。URL 生成功能集中在此模块中。
有关路由和 config/routes.rb 的一般信息,请参阅 ActionDispatch::Routing。
提示: 如果您需要从模型或其他地方生成 URL,那么 ActionDispatch::Routing::UrlFor 就是您需要的。继续阅读以了解介绍。通常,此模块不应单独包含,因为它通常由 url_helpers(例如 Rails.application.routes.url_helpers)包含。
根据参数生成 URL¶ ↑
您可能知道,一些函数,例如 ActionController::Base#url_for 和 ActionView::Helpers::UrlHelper#link_to,可以根据一组参数生成 URL。例如,您可能曾在视图中写过类似这样的代码
<%= link_to('Click here', controller: 'users',
action: 'new', message: 'Welcome!') %>
# => <a href="/users/new?message=Welcome%21">Click here</a>
link_to 以及所有需要 URL 生成功能的其他函数,实际上在底层都使用了 ActionDispatch::Routing::UrlFor。特别是,它们使用了 ActionDispatch::Routing::UrlFor#url_for 方法。您可以使用以下代码生成与上述示例相同的路径
include ActionDispatch::Routing::UrlFor url_for(controller: 'users', action: 'new', message: 'Welcome!', only_path: true) # => "/users/new?message=Welcome%21"
请注意 only_path: true 部分。这是因为 UrlFor 没有关于您的 Rails 应用正在服务的网站主机的任何信息。因此,如果您也想包含主机名,则必须同时传递 :host 参数
include UrlFor url_for(controller: 'users', action: 'new', message: 'Welcome!', host: 'www.example.com') # => "http://www.example.com/users/new?message=Welcome%21"
默认情况下,所有控制器和视图都可以访问 url_for 的特殊版本,该版本已经知道当前主机名是什么。因此,如果您在控制器或视图中使用 url_for,则无需显式传递 :host 参数。
为了方便起见,邮件程序也包含了 ActionDispatch::Routing::UrlFor。因此,在邮件程序中,您可以使用 url_for。但是,邮件程序无法访问传入的 Web 请求以派生主机名信息,因此您必须提供 :host 选项或使用 default_url_options 设置默认主机。有关邮件程序中 url_for 的更多信息,请参阅 ActionMailer::Base 文档。
根据命名路由生成 URL¶ ↑
UrlFor 还允许访问从命名路由自动生成的函数。例如,假设您的 config/routes.rb 中有一个“users”资源
resources :users
这会生成 users_path 方法等。默认情况下,此方法可从您的控制器、视图和邮件程序访问。如果您需要在其他地方(例如模型)访问此自动生成的函数,则可以通过在类中包含 Rails.application.routes.url_helpers 来实现
class User < ActiveRecord::Base include Rails.application.routes.url_helpers def base_uri user_path(self) end end User.find(1).base_uri # => "/users/1"
- #
- N
- O
- R
- U
类公共方法
new(...) Link
实例公共方法
route_for(name, *args) Link
允许调用直接或常规命名路由。
resources :buckets direct :recordable do |recording| route_for(:bucket, recording.bucket) end direct :threadable do |threadable| route_for(:recordable, threadable.parent) end
这会保留原始调用者的上下文,以确定是返回路径还是完整 URL,例如
threadable_path(threadable) # => "/buckets/1" threadable_url(threadable) # => "http://example.com/buckets/1"
url_for(options = nil) Link
根据提供的选项、default_url_options 和 config/routes.rb 中定义的路由生成 URL。支持以下选项
-
:only_path- 如果为 true,则返回相对 URL。默认为false。 -
:protocol- 连接的协议。默认为"http"。 -
:host- 指定链接应指向的主机。如果:only_path为 false,则必须通过显式提供或通过default_url_options提供此选项。 -
:subdomain- 使用tld_length将子域名与主机分开,指定链接的子域名。如果为 false,则从链接的主机部分删除所有子域名。 -
:domain- 使用tld_length将域名与主机分开,指定链接的域名。 -
:tld_length- TLD 由多少个标签组成,仅在使用:subdomain或:domain时使用。默认为ActionDispatch::Http::URL.tld_length,该值默认为 1。 -
:port- 可选地指定要连接的端口。 -
:anchor- 将附加到路径的锚点名称。 -
:params- 将附加到路径的查询参数。 -
:path_params- 仅用于路径的命名动态段的查询参数。如果未使用,它们将被丢弃。 -
:trailing_slash- 如果为 true,则添加一个尾部斜杠,例如"/archive/2009/"。 -
:script_name- 指定相对于域根目录的应用程序路径。如果提供,则会预先添加应用程序路径。
传递给 url_for 的任何其他键(:controller、:action 等)都会转发给 Routes 模块。
url_for controller: 'tasks', action: 'testing', host: 'somehost.org', port: '8080' # => 'http://somehost.org:8080/tasks/testing' url_for controller: 'tasks', action: 'testing', host: 'somehost.org', anchor: 'ok', only_path: true # => '/tasks/testing#ok' url_for controller: 'tasks', action: 'testing', trailing_slash: true # => 'http://somehost.org/tasks/testing/' url_for controller: 'tasks', action: 'testing', host: 'somehost.org', number: '33' # => 'http://somehost.org/tasks/testing?number=33' url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp" # => 'http://somehost.org/myapp/tasks/testing' url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp", only_path: true # => '/myapp/tasks/testing'
缺少的路由键可能会从当前请求的参数中填充(例如 :controller、:action、:id 以及放入路径的任何其他参数)。假设当前操作是通过 GET /users/1 达到的
url_for(only_path: true) # => '/users/1' url_for(only_path: true, action: 'edit') # => '/users/1/edit' url_for(only_path: true, action: 'edit', id: 2) # => '/users/2/edit'
请注意,第一个 url_for 调用未提供 :id 参数,并且该帮助方法使用了路由路径中的参数。 url_for 隐式使用的任何路径参数都可以像最后一个 url_for 调用所示那样被覆盖。
url_options() Link
在控制器中重写的钩子,用于通过 default_url_options 添加请求信息。应用程序逻辑不应进入 url_options。