views:

69

answers:

2

Hi All, new to Ruby so I'm still getting to grips with some of the Rails framework and how it relates. I've got a question about why my project behaves in a certain way.

I've created a basic layout, header the top of screen, side menu on the left, and a contents page for the main area. Nothing flash, bog standard as you can see from the code below. (Note, for whatever reason my code below doesn't want to get printed correctly on this page!!)


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

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
 <head>
  <title>MyLife</title>
  <%= stylesheet_link_tag 'main'%>
  <%= javascript_include_tag :defaults %>
  <%= yield :head %>
 </head>
 <body>
  <div id="container">
   <div id="header">
    <%=image_tag 'mylife.jpg', :alt => "MyLife" %>
   </div>
   <div id="sidemenu">
    <%= render :partial => 'layouts/menu'%>
   </div>
   <div id="content">
    <% if flash[:notice] -%>
     <div id="notice"><%= flash[:notice]%></div>
    <% end -%>
    <% if flash[:error] -%>
     <div id="error"><%= flash[:error]%></div>
    <% end -%>
    <%= yield %>
   </div> 
  </div>
 </body>
</html>

For the side bar, I've just got some links to a few pages that I want displayed in the contents area - again nothing flash as you can see below.


<ul>
 <!-- These links are standard for everyone - links to the Homepage, and to the Articles news page -->
 <li><%= link_to 'Home', index_url %></li>
 <li><%= link_to 'News', articles_path %></li>

 <hr size = "1" width = "90%" aligh = "left" />

 <!-- The remaining links will change depending on context, login state & credentials-->
 <% if is_logged_in? %>
  <li> Logged in as: <i><%= logged_in_user.username %> </i></li>
  <li><%= link_to 'My Profile', edit_user_path(logged_in_user) %> </li>
  <li><%= link_to 'Articles', articles_path %> </li>
  <li><%= link_to 'Logout',{:controller => 'account', :action => 'logout'}, :method => :post  %> </li>
 <% else %>
  <li><%= link_to 'Signup', :controller => 'users', :action => 'new' %> </li>
  <li><%= link_to 'Login', :controller => 'account', :action => 'login' %></li>
 <% end %>

 <% if logged_in_user and logged_in_user.has_role?('administrator') %>
  <hr size="1" width="90%" align="left">
  <li><b>Admin Options</b></li>
  <li><%= link_to 'Administer Users', users_path %></li>
  <li><%= link_to 'Edit Page', pages_path%></li>
 <% end %>

 <% if is_logged_in? and logged_in_user.has_role?('editor') %>
  <hr size="1" width="90%" align="left">
  <li><b>Editor Options</b></li>
  <li><%= link_to 'Edit News Articles', admin_articles_path%></li>
  <li><%= link_to 'Edit News Categories', admin_categories_path%></li>
 <% end %>
</ul>

My frustration (at being new to the Rails framework) is that when I click on anything Article related (News link, Articles link), the formatting of my page goes and that rendered page gets displayed without the side menu, or the header. All other links behave as expected, with the linked-to page displaying within the Contents pane. What's going on here?

I'm sure I"m missing something very silly, and I can't seem what - which is damn frustrating!

Thanks in advance for helping a newb...

A: 

If you put your layout code in app/views/layouts/application.html.erb then it will apply to your whole site unless explicitly overridden. You might find the resources below helpful:

John Topley
Thanks for pointing me to that place. I found my answer in 2.2.12 "To find the current layout, Rails first looks for a file in app/views/layouts with the same base name as the controller". I use scaffolding and it means there is automatically a file for each object I scaffold. Deleting those unwanted layouts solved the problem - thanks!!
mrbernz
A: 

It seems, you don't apply layout to some of your controllers/actions. By default, app/views/layouts/application.html.erb will be applied to all html actions. There're many ways to redefine layout, for example you may have layout nil in controller. Check here for details:
http://guides.rubyonrails.org/layouts_and_rendering.html

For more help, please post the source of controller in doubt

Nikita Rybak
Thanks for pointing me to that place. I found my answer in 2.2.12 "To find the current layout, Rails first looks for a file in app/views/layouts with the same base name as the controller". I use scaffolding and it means there is automatically a file for each object I scaffold. Deleting those unwanted layouts solved the problem - thanks!
mrbernz