Module: ActionView::RoutingUrlFor
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | actionview/lib/action_view/routing_url_for.rb |
Instance Attribute Summary
- #optimize_routes_generation? ⇒ Boolean readonly private
Instance Method Summary
-
#url_for(options = nil)
Returns the URL for the set of
options
provided. - #_generate_paths_by_default private
- #_routes_context private
- #ensure_only_path_option(options) private
- #url_options Internal use only
Instance Attribute Details
#optimize_routes_generation? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actionview/lib/action_view/routing_url_for.rb', line 134
def optimize_routes_generation? controller.respond_to?(:optimize_routes_generation?, true) ? controller.optimize_routes_generation? : super end
Instance Method Details
#_generate_paths_by_default (private)
[ GitHub ]# File 'actionview/lib/action_view/routing_url_for.rb', line 139
def _generate_paths_by_default true end
#_routes_context (private)
[ GitHub ]# File 'actionview/lib/action_view/routing_url_for.rb', line 130
def _routes_context controller end
#ensure_only_path_option(options) (private)
[ GitHub ]# File 'actionview/lib/action_view/routing_url_for.rb', line 143
def ensure_only_path_option( ) unless .key?(:only_path) [:only_path] = _generate_paths_by_default unless [:host] end end
#url_for(options = nil)
Returns the URL for the set of options
provided. This takes the same options as url_for
in Action Controller (see the documentation for ActionDispatch::Routing::UrlFor#url_for). Note that by default :only_path
is true
so you’ll get the relative "/controller/action"
instead of the fully qualified URL like "http://example.com/controller/action"
.
Options
-
:anchor
- Specifies the anchor name to be appended to the path. -
:only_path
- If true, returns the relative URL (omitting the protocol, host name, and port) (true
by default unless:host
is specified). -
:trailing_slash
- If true, adds a trailing slash, as in"/archive/2005/"
. Note that this is currently not recommended since it breaks caching. -
:host
- Overrides the default (current) host if provided. -
:protocol
- Overrides the default (current) protocol if provided. -
:user
- Inline HTTP authentication (only plucked out if:password
is also present). -
:password
- Inline HTTP authentication (only plucked out if:user
is also present).
Relying on named routes
Passing a record (like an Active Record) instead of a hash as the options parameter will trigger the named route for that record. The lookup will happen on the name of the class. So passing a Workshop object will attempt to use the workshop_path
route. If you have a nested route, such as admin_workshop_path
you’ll have to call that explicitly (it’s impossible for url_for
to guess that route).
Implicit Controller Namespacing
Controllers passed in using the :controller
option will retain their namespace unless it is an absolute one.
Examples
<%= 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( = nil) case when String when nil super(only_path: _generate_paths_by_default) when Hash = .symbolize_keys ensure_only_path_option( ) super( ) when ActionController::Parameters ensure_only_path_option( ) super( ) when :back _back_url when Array components = .dup = components. ensure_only_path_option( ) if [:only_path] polymorphic_path(components, ) else polymorphic_url(components, ) end else method = _generate_paths_by_default ? :path : :url builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.public_send(method) case when Symbol builder.handle_string_call(self, ) when Class builder.handle_class_call(self, ) else builder.handle_model_call(self, ) end end end
#url_options
# File 'actionview/lib/action_view/routing_url_for.rb', line 124
def # :nodoc: return super unless controller.respond_to?(: ) controller. end