Forum: Ruby on Rails layouts + routes = confused

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C0f75c054e0191ad24dd655b2e1b4532?d=identicon&s=25 Reacher (Guest)
on 2007-05-09 17:25
(Received via mailing list)
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:

<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?
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-05-09 17:35
(Received via mailing list)
in ApplicationController, for setting the layout across the site:

layout 'layout_name'

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

C0f75c054e0191ad24dd655b2e1b4532?d=identicon&s=25 Reacher (Guest)
on 2007-05-09 17:44
(Received via mailing list)
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" /
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-05-09 18:29
(Received via mailing list)
It's "/stylesheets"

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

D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-05-09 19:19
Jason Roelofs 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.
D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2007-05-10 13:01
(Received via mailing list)
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" %>
(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

('s your toolbar code...)
<div id="latest_posts"
  <%= yield :latest_posts %>

<% 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:
This topic is locked and can not be replied to.