polymorphic_url
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3 (0)
- 2.1.0 (2)
- 2.2.1 (9)
- 2.3.8 (-1)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
polymorphic_url(record_or_hash_or_array, options = {})
public
Constructs a call to a named RESTful route for the given record and returns the resulting URL string. For example:
# calls post_url(post) polymorphic_url(post) # => "http://example.com/posts/1" polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1" polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1" polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
Options
- :action - Specifies the action prefix for the named route: :new or :edit. Default is no prefix.
- :routing_type - Allowed values are :path or :url. Default is :url.
Examples
# an Article record polymorphic_url(record) # same as article_url(record) # a Comment record polymorphic_url(record) # same as comment_url(record) # it recognizes new records and maps to the collection record = Comment.new polymorphic_url(record) # same as comments_url()
Universal partial
polymorphic_url is very useful if you want to create an universal partial that works for more than 1 type of object passed to it.
For example in you sidebar you might have a _sidebar.html.erb partial that’s supposed to display links to “Edit” and “Delete” actions. You can write it in such a way that it can be reused for different types of objects (in the example below we pass either a Post or a Note).
your_template.html.erb
<%= render :partial => 'shared/sidebar', :locals => { :obj => Post.new -%>
other_template.html.erb
<%= render :partial => 'shared/sidebar', :locals => { :obj => Note.new -%>
_sidebar.html.erb
<%= link_to "Edit", polymorhpic_url(obj, :action => 'edit') -%> <%= link_to "Delete", polymorphic_url(obj), :method => :delete -%>
polymorphic_url and namespaces and nested resources
You can use polymorphic_url with namespaces and nested resources using array as parameter:
polymorphic_url([:admin, @post])
will return:
admin_post_url(@post)
Easier Universal Partials
@hosiawak provides a great example of how to use polymorphic_path to produce universal partials. You can actually simply his example even more with the following:
<%= link_to 'Edit', edit_polymorphic_path(obj) %> <%= link_to 'Delete', obj, :method => :delete %>
So the things to note are that in addition to polymorphic_path, Rails also provides an “edit_” version just like on your resources so you can use that instead of specifying the action specifically. The second thing to remember is if you pass just the raw object as the path then then rails will automatically wrap it in a call to polymorphic_path.
Anchor option
It’s not documented, but :anchor is an option.
polymorphic_path(commentable, :anchor => 'comments')
will return:
/article/1#comments
Moved to ActionDispatch::Routing::PolymorphicRoutes
In Rails 3, this has moved to ActionDispatch::Routing::PolymorphicRoutes.
Bug? does not encode options
polymorphic_path(item,options) =polymorphic_path(item)+hash_to_url_query(options)
def hash_to_url_query(hash) url = [] hash.each{|k,v| url << "#{k}=#{v}"} "=" + (url * '&') end