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?!
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With