# Layouts + routes = confused

**URL:** <https://www.ruby-forum.com/t/layouts-routes-confused/103718>\
**Category:** Rails\
**Created:** [May 9, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/layouts-routes-confused/103718 "2007-05-09T17:25:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [May 9, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/layouts-routes-confused/103718/1 "2007-05-09T17:25:51Z")

</div>

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:

..... ....
\<%= 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?

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [May 9, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/layouts-routes-confused/103718/2 "2007-05-09T17:35:52Z")

</div>

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](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000129)

Jason

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [May 9, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/layouts-routes-confused/103718/3 "2007-05-09T17:44:55Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [May 9, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/layouts-routes-confused/103718/4 "2007-05-09T18:29:57Z")

</div>

It’s “/stylesheets”

And to be safe, use the stylesheet\_link\_tag helper instead of writing it  
out  
yourself.

Jason

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [May 9, 2007, 7:19pm UTC](https://www.ruby-forum.com/t/layouts-routes-confused/103718/5 "2007-05-09T19:19:00Z")

</div>

Jason R. wrote:

> It’s “/stylesheets”
> 
> And to be safe, use the stylesheet\_link\_tag helper instead of writing it  
> out  
> yourself.
> 
> Jason

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.

---

<div class="post-metadata">

**Author:** ![reacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/0ea827/32.png) [@reacher](https://www.ruby-forum.com/u/reacher)\
**Post date:** [May 10, 2007, 1:01pm UTC](https://www.ruby-forum.com/t/layouts-routes-confused/103718/6 "2007-05-10T13:01:57Z")

</div>

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  
then:

#application.rhtml  
(…here’s your toolbar code…)

#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 😉

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

> **[Strut Your Structs — err.the\_blog](http://errtheblog.com/post/28)**
>
> Err the Blog: Ruby and Rails from the front.
