Archives

All posts for the month August, 2014

rails

One of the great strengths of Rails is the ability to create layouts that house various views of content. You create a layout with a basic HTML structure and then simply drop in your new content based on the view you are rendering. One of the simplest layouts is one that just renders the view that takes up the whole page and this is accomplished by the auto generated
app/views/layouts/application.html.erb.

The highlighted line is the important one. That <%= yield %> tells Ruby on Rails that whatever view you load just drop it right there. That’s great! Now we have a simple base layout that houses our imports and basic structure and CSS and we simply drop in our own content inside of this.

Let’s say we want to take it a step further. What if we want a left and a right side bar which is unique to each of our views? We just need to change our basic layout a little bit to add those columns and then update our view template code to take advantage of our new structure.

Yield Sections

app/views/layouts/application.html.erb

app/views/MODEL/new.html.erb

Following the suggestion of Obie Fernandez and Kevin Faustino in their book The Rails 4 Way, I have placed the two named structures of the layout first. It makes it easy to distinguish what the smaller sections will contain before getting to the meat of the content. Everything that isn’t labeled as content_for will be dropped in that unnamed yield in the layout.

Render Partials

That’s all well and good but what if you don’t want a specific set of sidebar content for every single view. Maybe a shared partial for the right bar will work great. In that case you’ll need to use the render method in your template to generate the template for you. Here is the example from above but using a shared right side bar template.

app/views/layouts/application.html.erb

app/views/shared/_right.html.erb

Render Partials with Variables

You can even pass in variables to your partials. Let’s say our right side bar takes advantage of some user data. We just pass the user variable to the render with a comma and a hash type variable being passed.

app/views/layouts/application.html.erb

app/views/shared/_right.html.erb

Pretty cool, right? I hope this introduction to layouts and partials was helpful. If there are any problems with my examples or if you have any questions please feel free to leave a comment below.