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 together.

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:

[header] [menubar] [toolbar][main content] [footer]

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:

<html> ..... <body> .... <div id="menubar"> <%= yield :menubar %> ... <div id="main"> <%= 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: http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000129

Jason

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:

<link rel="stylesheet" type="text/css" href="stylesheets/default.css" /

It’s “/stylesheets”

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

Jason

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": <div id="menubar"> <%= render :partial => "shared/menubar" %> </div> (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 then:

#application.rhtml (....here's your toolbar code...) <div id="latest_posts"   <%= yield :latest_posts %> </div>

#forums.rhtml <% 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: http://errtheblog.com/post/28