Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested layouts in ruby on rails

I'm new to rails and am trying to work out how to get nested layouts working; I'm assuming they're a bit like .net master pages?

I've followed this guide and I've created an application.erb.html in my layout directory which contains this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title><%= @page_title or 'Page Title' %></title>
  <%= stylesheet_link_tag 'layout' %>
  <style type="text/css"><%= yield :stylesheets %></style>
</head>
<body>

<%= yield(:content) or yield %>

</body>
</html>

and have modified one of my existing layouts to this:

<% content_for :stylesheets do %>

<% end -%>

<% content_for :content do %>
  <p style="color: green"><%= flash[:notice] %></p>
  <%= yield %>
<% end -%>

<% render :file => 'layouts/application' %>

When I go to one of my views in the browser, absolutely nothing is rendered; when I view source there is no html.

I'm sure there's something elementary I've missed out, can anyone point it out please?!

like image 947
Charlie Avatar asked Apr 12 '09 15:04

Charlie


People also ask

How do you use nested layouts in Rails?

Nesting layouts is actually quite easy. It uses the content_for method to declare content for a particular named block, and then render the layout that you wish to use. So, that's the normal application layout.

What is the layout in Ruby on Rails?

A layout defines the surroundings of an HTML page. It's the place to define a common look and feel of your final output. Layout files reside in app/views/layouts. The process involves defining a layout template and then letting the controller know that it exists and to use it.

How can you tell Rails to render without a layout?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.


1 Answers

I've worked out the solution, although it's not what's given in this article

I've replaced this line

<% render :file => 'layouts/application' %>

with

<%= render :file => 'layouts/application' %>

I'm not sure if the article is wrong, or I've found the wrong way to fix it! Please let me know!

Cheers

like image 169
Charlie Avatar answered Nov 05 '22 01:11

Charlie