views:

25

answers:

3

I am trying to design my ruby on rails app at the moment. I created views/layouts/posts.html.erb which styled my PostsController's views.

I want to add a main bar that is always at the top of the page no matter what view the user is looking at. I thought this was what the views/layouts/application.html.erb was for.

This seems to agree with me: http://jacqueschirag.wordpress.com/2007/08/02/rails-layout-and-nested-layout-basics/

  • The entire Rails application (all views of all controllers) will use this layout:

    views/layouts/application.rhtml

  • All views within a single controller will use this layout. For example, the layout for weclome_controller.rb will use this layout. Notice, the ‘_controller’ is left off for the layout:

    views/layouts/welcome.rhtml

What am I doing wrong?

Here is what I have in the body of my application.html.erb

  <div id="top-bar">
      <div id="user_nav">
        <% if current_user %>
          <%= link_to "My Profile", current_user %>
          <%= link_to "Logout", logout_path %>
        <% else %>
          <%= link_to "Register", new_user_path %>
          <%= link_to "Login", login_path %>
        <% end %>
      </div>
    </div>
    <%= yield %>
+1  A: 

Well the information is correct - 'views/layouts/application.html.erb' should be working just fine.

Have you included the "main bar" content using <%= ..... %> (i.e. are you missing the = sign in your layout file?) And, have you included the <%= yield %> statement somewhere in the layout file?

Also, what version of Rails are you using?

You should find this Layouts and Rendering in Rails very helpful in getting to the basics of rails views and layouts

Zabba
I have added the body of my application.html.erb file to the main question. I am using Rails 3. I have just realised that I have all the <html><head></head><body></body></html> in my post.html.erb layout. Am I supposed to?
GreenRails
+1  A: 

Both your layouts, application.html.erb and posts.html.erb should render the main bar as a partial:

<%= render :partial => 'layouts/main_bar' %>

All layouts contain the <html> element that wraps your page; you don't have one layout wrapping around the other.

Andrew Vit
Thanks but with that code I am getting: "Missing partial layouts/main_bar"
GreenRails
Err.. I forgot to put in the underscore in the filename. Thanks for you help. I'll mark this answer as the correct one since you showed how to do it.
GreenRails
+1  A: 

I'm confused… do you still have views/layouts/posts.html.erb ? If that's the case all views rendered from PostsController will use this layout and not views/layouts/application.html.erb

Yannis
Oh, ok. I just had a misunderstanding of how it worked.
GreenRails
This "answer" should have been a comment
Zabba