Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - override <head> page element when using a partial or a specific controller view

Is there a way to append or override the <head> page element inside a rails view?

Suppose I have something I only want to include in a particular view's <head>, and I am using the application.html.erb in order to render the rest of my views. In this case I do not want to discard application.html.erb but instead just append to it's head element for one particular page and nothing else.

like image 324
Andrew Lauer Barinov Avatar asked Apr 19 '12 09:04

Andrew Lauer Barinov


1 Answers

In your application.html.erb:

<head>
  <% if content_for? :for_head %>
    <%= yield :for_head %>
  <% end %>
</head>

In your "specific" view:

<% content_for :for_head do %>
  Something-to-put-in-head
<% end %>

:for_head isn't predefined: naming it is up to you. It could be anything.

like image 61
jdoe Avatar answered Sep 18 '22 19:09

jdoe