Posts Tagged ‘partials’

Rails 2.0 Patrials and Collections part2

// March 19th, 2009 // No Comments » // Rails

In part one of this article we went over 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. We’ll see how you can set additional local variables in your partial. This article is short and assume you have read part1 of this article.

Outline for this article

  1. How do we set local variables in Rails Partials?
  2. Using Rails Partials in a loop?
  3. Is there a point to the Spacer Template in Rails Partials?

How do we set local variables in Rails Partials?
Ok, so we know that we can pass in an object to a partial with the

 :o bject

symbol. Great, but what if you want to use more variables in the partial.
This can be done using the

:locals

symbol in the render helper. For example if go back to the example given in part1

<%= render :partial => "shared/testpartial", :o bject => @name , :locals => {:testvar => @any_object_you_want } %>;

Just make sure you know the name of the local variables you are setting.

Using Rails Partials in a loop?
With partials we can format create modular views. Sometimes you’ll find the need to
render a partial many time over. On way you can do this is to run the partial in a for loop.
Rails has a shortcut for this. You can use the symbol

:collection

The symbol takes a collection of objects that the partial takes! The example from part1 took a string object. So to use the collection we can pass it a list of string objects like the following

<%= render :partial => "shared/testpartial",
:collection => %w{ Mez Adam Azmara John} %>;

This is a very useful feature, if used correctly your views show look clean. This makes maintenance
of code very manageable and enjoyable.

Is there a point to the Spacer Template in Rails Partials?
There is a option the render helper can take called

:spacer_template

It lets you specify a template that will be rendered between each of the elements in the collection.
Basically, it renders another parital. Here is how to use it.

<%= render :partial =&gt; "shared/testpartial",
:collection => %w{ Mez Adam Azmara John},
:spacer_template => "shared/another_partial" %>;

Note, this assumes you have a “_another_partial.html.erb” handy. My thinking on this is a little off. I am not sure if you really need to define another partial to display in between partials. Why not just define the content of the spacer template in the first partial? Give some good examples if you think of any!

That concludes this article. I hope you enjoyed it!

Rails 2.0 Patrials and Collections part1

// March 19th, 2009 // No Comments » // 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!

-->

Categories