- U
執行個體公開方法
url_for(options = nil) 連結
傳回所提供的 options
組合的 URL。這採用與 Action Controller 中 url_for
相同的選項(請參閱 ActionDispatch::Routing::UrlFor#url_for
的文件)。請注意,預設值 :only_path
為 true
,因此你取得的將是相對的 "/controller/action"
,而非完全限定的 URL,例如 "http://example.com/controller/action"
。
選項
-
:anchor
- 指定要附加至路徑的錨點名稱。 -
:only_path
- 若為 true,則傳回相對 URL(省去協定、主機名稱和埠)(若未指定:host
則預設為true
)。 -
:trailing_slash
- 若為 true,則會加上尾斜線,例如"/archive/2005/"
。請注意,目前不建議這樣做,因為它會中斷快取。 -
:host
- 如果有提供,將覆寫預設(目前)的主機。 -
:protocol
- 如果有提供,將覆寫預設(目前)的協定 -
:user
- 內嵌 HTTP 驗證(僅在同時存在:password
時摘取)。 -
:password
- 內嵌 HTTP 驗證(僅在同時存在:user
時摘取)。
依賴於命名路徑
如果傳遞記錄(例如 Active Record)而非雜湊作為選項參數,則會觸發該記錄的命名路徑。查詢將會針對類別的名稱執行。因此,傳遞 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
來源:查看 | 前往 GitHub 查看
# 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