Layouts + routes = confused

I’m new to RoR and the tutorials I’ve found on the web, while helpful,
aren’t as “complete” as I’d like them. I understand the basics when
it comes to layouts and routes, but I’m trying to get them to work

MY first task was to implement a user authentication system
(registration/login/logout/change_password). This works fine with my
User controller/model/views. Now, I’ve made a default layout that
resembles the following:

[toolbar][main content]

I made another controller called main that i am rendering into the
main content part of my layout. I’ve added a route to go to main/
index when at my url, so it’s the default page. The layout HTML looks
like this:

..... ....
<%= yield :menubar %> ...
<%= yield %> ...

Now in my views/main/index.rhtml I have:

<% content_for :menubar do %>
<% link_to “Login”, :controller => “user”, :action => “Login” %>
<% end %>

This works great and puts the link into the menubar div. Only problem
now is that when i click it the site goes to /user/Login and I lose my
layout. Is there something I should be putting in my login function
in my user class? Or is there another route I should have?

in ApplicationController, for setting the layout across the site:

layout ‘layout_name’

That can be overridden in any of your controllers as needed:


If you mean adding a layout line to my user controller, i already
did. i think it may be the path to my stylesheet because when i click
the link then view the source of the page, the layout is all there but
it’s not being rendered (as if it can’t see the stylesheet). my
stylesheet is in public/stylesheets/default.css and the link in the
layout is:

It’s “/stylesheets”

And to be safe, use the stylesheet_link_tag helper instead of writing it


Jason R. wrote:

It’s “/stylesheets”

And to be safe, use the stylesheet_link_tag helper instead of writing it


This man is correct. It needs to starts with a slash so the browser can
find it easily. Or just do:

<%= stylesheet_link_tag ‘default’ %>

Also if you name your layout “application.rhtml” it will automatically
be used accross all controllers unless you tell it otherwise. This is
usually the approach I take.

I would take a different approach.

  1. use application.rhtml for your main layout, and don’t specify
    another layout in any controller as long as you dont want a controller
    to have a totally different layout. And as your layout is just the
    basic skeleton, i dont think you will…

  2. if your menubar will be about the same for every controller, i
    wouldn’t use content_for, but just put the menubar in a partial and
    render that in the layout. Put it in a subfolder of your views
    directory called e.g. “shared”:

<%= render :partial => "shared/menubar" %>
(unsure weither you need the underscore (_menubar) when giving a path to a partial, try it out)

But another common problem is, that you might want to show an entirely
different menu, or e.g. a Box under the toolbar that shows the latest
posts, but only in the ForumsController… here’s what you would do

(…here’s your toolbar code…)

<% content_for :latest posts do %>
(fill in code/HTML here)
<% end %>
<%= render :file => “layouts/application.rhtml”

whats happening here is the following: If an action in
ForumsController is called, rails will not render layouts/
application.rhtml, but layouts/forums.rhtml … in this layout, you
only fill the content_for :latest_posts, and then render :file the
“real” layout in application.rhtml
in appplication.rhtml, your :latest_posts will then be put in place.

If you would call an action from another controller, forums.rhtml
would not be rendered, but application.rhtml directly. so no
latest_posts :wink:

i know this sounds weird, read this blog post, it helped me a lot:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs