3

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.