Rails 2.0 Patrials and Collections part1

// March 19th, 2009 // Rails

Often in web applications, the same data is displayed in more than one location. Usually this is done by copying code between different template pages. Using Rails we can avoid this and remain true to the Rails principles of “never repeating ourselves”. This is accomplished by using Rails partials.

In part one of this article we’ll see how to create and use partials. Part two will take what we learned from part one and see how we can use collections to make partials even more useful. Note, more info can be found from the book “Agile Web Development with Rails” in the chapter that deals with ActionView.

Outline for this article

  1. What the heck are Rails Partials?
  2. How do we create Rails Partials?
  3. How to use Rails Partials?

What the heck are Rails Partials?
Rails Partials are basically subroutines in their own right. You can use them many times and in many different templates. If you want, you can even pass in objects to render as parameters. A partial template looks just like a regular templates in Rails with the exception of the name of the file containing the partial. The name of the file containing the template code must start with an underscore character.

How do we create a Rails Partials?
I learn best by examples. So that’s what we’ll do here! Let take a simple example that serves no purpose but to learn from. Lets create a simple partial template that will take a string object and simply render the string. In a file named “_testpartial.html.erb”.

Some background information to keep in mind. you might be wondering where do you store this partial file? Well that depends. If you want to use this partials in views from multiple controllers, then what I like to do is create a folder called “shared” in the views directory. So go ahead and do that now. Are you done yet? Ok, lets keep moving.

Hello, <%=testpartial%>;

Keep in mind that the partial in this example expects a string object. Well now you might be wondering how does the partial know the name of the object being passed in? It uses the name of the partial as a variable name. Since this simple partial is called “_testpartial.html.erb”, we can use the variable “testpartial”. Now that we have this partial setup and created lets go and use it!

How to use Rails Partials?
Using a partial is really simple. You can do this in two places. The first way is in the controller like the following.

def index
@name = "Meseret Gebre"
render :partial =&gt; "shared/testpartial", :o bject =&gt; @name
end

The second way is in the view. For example if we setup the instance variable in the controller like the following.

def index
#var to be used in view.
@name = "Meseret Gebre"
end

Then in the view “index.rhtml” we can render the partial by placing the following code somewhere in the view.

<%=render :partial => "shared/testpartial", :o bject => @name%>;

Thats Rails partials in a nutshell. Remember in part2 of this article, we’ll look at using partials to render a collections of objects! I hope you learned something cool here and remember to never repeat yourself! Any questions or comments are welcomed!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Buzz

Leave a Reply

-->

Categories