views:

435

answers:

2

I don't know what's the best way to doing this.

On my application.html.erb I define a header div.

My default controller ( root ) is a homepage controller. And I wish that if I'm at index of the homepage the header is rendering with some content, but all other controllers render another content inside that header.

How can I make a condition in that header div to render different content based on the controller that's being rendered?

A: 

You can check controller with params[:controller] and action with params[:action] in your layout

application.html.erb

   <div id="header">

        <% if params[:controller] == 'homepage controller' %>
           // homepage controller content
        <% else %>
           //other than homepage controller content
        <% end %>

    </div>
Salil
That's completely against all the rails conventions.
Damien MATHIEU
+3  A: 

You can use the content_for helper to define a block of markup in individual view templates that's yielded to from the application layout.

application.html.erb:

<div id="header">
  <%= yield :header %>
</div>

Then include code like this anywhere within a controller's view template:

<% content_for :header do %>
  This is a controller-specific header.
<% end %>

Note that you can define as many of these blocks as you like, so you could have a conditional sidebar etc. as well.

John Topley