irl_irl
irl_irl

Reputation: 3975

I thought views/layouts/application.html.erb was meant to apply to all layouts?

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 %>

Upvotes: 0

Views: 5295

Answers (3)

Zabba
Zabba

Reputation: 65517

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

Upvotes: 1

Yannis
Yannis

Reputation: 5426

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

Upvotes: 1

Andrew Vit
Andrew Vit

Reputation: 19259

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.

Upvotes: 5

Related Questions