Code refactoring: generic partials

August 30, 2009 at 3:10 pm Leave a comment

When making project of any size you will most probably find yourself repeating code from one model in the other, be it similarities in models, controllers or views. This time I would like to explain how you can take out any repeated code in your views by using partials.

In order to call a partial from your view, you will need to use a render(:partial) method, like this:

<%= render :partial => "common/tags", :locals => { :thing => @artist } %>

1 – So, “common/tags” tells Rails to look for your partial called _tags.html.erb (remember the underscore in front – this is an easy visual way to say that the view template is a partial) in directory app/views/common.

2 – The :locals hash sends all needed variables to the partial. In our case we are sending @artist variable as a local variable called thing. You can send as many local variables as you need.

Now, let’s look at the partial:

_tags.html.erb:

	 <b>Tags:</b>
	 <% thing.tags.uniq.each do |tag|%>
	 <%= link_to tag.name.capitalize, tag_path(tag.name) %>
	 <% end %>
	 <%= link_to "Add more tags", edit_polymorphic_path(thing) %>
     <% end %>

1 – You see that the partial works with whatever the view passed to it in the local thing variable. As it’s a local variable there is no @ in front.
2 – As the partial does not really know what thing really is (and doesn’t need to in our case, apart from the fact that the thing should have tags method), if you want to include links in the partials, there are some easy to use methods for you:

link_to "Show this thing to me", thing
link_to "Edit this thing", edit_polymorphic_path(thing)

1 – The first notation is very useful short form of link_to – you can pass whatever object in place of the link and Rails will generate the right link to this object’s show method (it works with RESTful resources).

2 – The second is the same but for editing any object. Just use edit_polymorphic_path(thing). It will create an edit path for your object automatically. You can read more about polymorphic routes here.

So, this way you can make your partials generic and not dependent on the type of object being passed to it. If you will need to know what the thing being passed really is an object of, you can always check the thing.class.name property, which will return a string with the name of its class. But if you need to branch your code based on class of the object passed, you might not need to move it to a partial in the first place.

Advertisements

Entry filed under: Uncategorized. Tags: , , , .

Best Resources for Rails Updating Ruby / Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent posts

Starting to learn Rails?

Kindle

Get Kindle - the best e-book reader, that I personally use, and the only one that you can read on the beach - very useful: Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)

%d bloggers like this: