Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Layout Rendering with controller condition

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?

like image 224
Victor Martins Avatar asked Sep 19 '26 19:09

Victor Martins


1 Answers

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.

  • Layouts and content_for Railscast
like image 154
John Topley Avatar answered Sep 22 '26 09:09

John Topley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!