module ActionDispatch::Routing::PolymorphicRoutes
def polymorphic_mapping(record)
def polymorphic_mapping(record) if record.respond_to?(:to_model) _routes.polymorphic_mappings[record.to_model.model_name.name] else _routes.polymorphic_mappings[record.class.name] end end
def polymorphic_path(record_or_hash_or_array, options = {})
def polymorphic_path(record_or_hash_or_array, options = {}) if Hash === record_or_hash_or_array options = record_or_hash_or_array.merge(options) record = options.delete :id return polymorphic_path record, options end if mapping = polymorphic_mapping(record_or_hash_or_array) return mapping.call(self, [record_or_hash_or_array, options], true) end opts = options.dup action = opts.delete :action type = :path HelperMethodBuilder.polymorphic_method self, record_or_hash_or_array, action, type, opts end
def polymorphic_path_for_action(action, record_or_hash, options)
def polymorphic_path_for_action(action, record_or_hash, options) polymorphic_path(record_or_hash, options.merge(action: action)) end
def polymorphic_url(record_or_hash_or_array, options = {})
polymorphic_url(Comment) # same as comments_url()
# the class of a record will also map to the collection
polymorphic_url(record) # same as comments_url()
record = Comment.new
# it recognizes new records and maps to the collection
polymorphic_url(record) # same as comment_url(record)
# a Comment record
polymorphic_url(record) # same as article_url(record)
# an Article record
==== Functionality
For all of these options, see the documentation for {url_for}[rdoc-ref:ActionDispatch::Routing::UrlFor].
# => "http://example.com/my_app/blogs/1/posts/1#my_anchor"
polymorphic_url([blog, post], anchor: 'my_anchor', script_name: "/my_app")
# => "http://example.com/blogs/1/posts/1#my_anchor"
polymorphic_url([blog, post], anchor: 'my_anchor')
is given below:
things as :anchor or :trailing_slash. Example usage
Also includes all the options from url_for. These include such
Default is :url.
* :routing_type - Allowed values are :path or :url.
:new or :edit. Default is no prefix.
* :action - Specifies the action prefix for the named route:
==== Options
polymorphic_url(Comment) # => "http://example.com/comments"
polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
polymorphic_url(post) # => "http://example.com/posts/1"
# calls post_url(post)
resulting URL string. For example:
Constructs a call to a named RESTful route for the given record and returns the
def polymorphic_url(record_or_hash_or_array, options = {}) if Hash === record_or_hash_or_array options = record_or_hash_or_array.merge(options) record = options.delete :id return polymorphic_url record, options end if mapping = polymorphic_mapping(record_or_hash_or_array) return mapping.call(self, [record_or_hash_or_array, options], false) end opts = options.dup action = opts.delete :action type = opts.delete(:routing_type) || :url HelperMethodBuilder.polymorphic_method self, record_or_hash_or_array, action, type, opts end
def polymorphic_url_for_action(action, record_or_hash, options)
def polymorphic_url_for_action(action, record_or_hash, options) polymorphic_url(record_or_hash, options.merge(action: action)) end