Quantcast
Channel: Good notes posted by hosiawak
Viewing all articles
Browse latest Browse all 8

Universal partial (ActionController::PolymorphicRoutes#polymorphic_url)

$
0
0

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-%>

Viewing all articles
Browse latest Browse all 8

Trending Articles