Layout tips using Ruby on Rails

When you don’t need a sidebar

One issue I encountered with using my approach to Rails layout is when my template doesn’t have a sidebar. Since I have already defined a ‘div’ tag for the sidebar in the layout file, the generated HTML page will have an empty ‘div’ tag. If you use CSS (you use CSS, right?), a border for example to the ‘div’ sidebar tag, you will have a hanging horizontal bar in your output. Another issue when I want the ‘main’ content to occupy the whole width of the page.

My solution is to have a condition for the ‘sidebar’ in the layout file and let each template specify the condition.

<!-- layout file -->
<% unless @wide_screen %>
 <div id="sidebar">
   <div class="content">
	 <%= yield :sidebar %>
   </div> <!-- content -->
 </div>  <!-- secondary -->
<% end %>
<!-- template file that has no sidebar -->
<%
 @page_id = 'No sidebar'
 @wide_screen = 'wide'
%>
<% content_for('main') %>
 Dynamic main content.
<% end %>

I use the value of the ‘@wide_screen’ variable as a class attribute of the ‘body’ tag. Then, I use CSS to expand the ‘main’ content as necessary.

#main {
  width: 75%;
}

body.wide #main {
 width: 100%;
}

A layout for everything

Everytime you use scaffold, a layout file is generated along with your controller. If the name of your controller is ‘EmployeesController’, the layout file is ’employees.rhtml’. This is the default setup but a typical web app would use a single layout for most, if not all, controllers. There are several ways to specify the layout to be used in your application.

1. Specify each layout in your controller

class AccountController &lt; ApplicationController
  layout 'employees'
end

This tells Rails to use the layout ’employees.rhtml’ for all templates used by ‘AccountController’. You can also use this technique to allow themes – a common requirement in content management systems. You can have one controller that handles read-access to data by any users. The public display can then be changed to a different theme by an authorized user (handled by a another controller). In your controller, dynamically specify the layout to be used.

class ViewController < ApplicationController layout :template def template # return the layout to be used end end [/sourcecode] 2. Specify the layout in your application controller. [sourcecode language='ruby'] class ApplicationController

15 thoughts on “Layout tips using Ruby on Rails

  1. Personally, I use the render method quite a lot. I often find it necessary to render a specific template for certain actions in a controller where the controller layout wouldn’t make sense.

    Nice articles on the layout, how about a few on custom form builders? Damn, I hate them forms but they are the bread and butter of web development. Bonus points for a custom form builder that renders for multiple models in a single form =)

  2. Nice post, Greg.

    In keeping with the spirit of convention-over-configuration, I think it would be more practical to scrap the content_for (‘main’) part, and just rather use the default render space (i.e. yield :layout in your layout) to put the main contents of the page.

  3. Awesome although I don’t normally employ this technique. It may as well be more useful in other applications in the future.

  4. I like adding classes to the body tag to drive things from special css for the entire page to menu highlighting. You mention that you use this method as well (right above your css above) but you don’t say HOW. I’d love to know…

  5. Hi Lorin,

    I have this code in the layouts/admin.rhtml

    @page_id, :class => @screen_size}.stringify_keys) %> >

    Then, in the template, I specify the value of @screen_size.

    < % @screen_size = 'wide' %>

  6. Hi Greg,

    thanks for the great insight into layout. I was wondering if we can use session identification in layouts. For example,
    then show navigation links: A ELSE show navigation links B .

    Forgive me about the syntax🙂 just for the sake of understanding.

    I tried this approach but i am getting an error that you have not spcified ‘nil’ user type. It would be great if you can guide me on this.

    Thanks.

  7. hi greg

    Sorry if I posted a comment here. I just want to apologize if raichu does not like my blog or other people here. Well I used that blog just for my reference for a few years when Im learning ruby on rails at least for me I learn a lot by doing it. If I am dumbass according to raichu, well its ok for what you called me. I just want to tell you that If I am a dumb person I cannot able to find a job or learn ruby or other web development technologies. But since I am able to finish my degree with my own knowledge and able to find a job that is related to me and so I have said to myself that I am not dumb . I hope people like him have a successful carrer in their lives not by calling and insulting other people. Hope this message will not offend raichu and others. If you have time come visit on my site : webpinoyonline.com. thanks so much and have a nice day

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