October 16th, 2010, 11:47 PM
Rails: Modifying <head> to include various metas and title tags
I've been learning Ruby these past few days and I'm really liking it. I'm still sort of confused by rails.
I'm trying to integrate a block of meta tags (along with one title tag) into the header of each one of my pages.
The tags & title for every page are different.
Based on the ERB template mechanism that's already running on this site, the path:
contains ERB's of all my html files: index.html.erb, about.html.erb, contact.html.erb, etc.
...WEBSITE/stable/app/views/layouts, I find a file named application.html.erb containing the following:
What is this <%= yield :extra_header_tags %>?
<%= yield :extra_header_tags %>
<%= stylesheet_link_tag 'jquery.lightbox-0.5' %>
<%= stylesheet_link_tag 'style.css' %>
<%= yield :stylesheets %>
<%= yield %>
And how do I integrate my meta's in such a way that they're different for every page?
I guess I'm supposed to define some variables and then point them to someplace but I'm not sure how, any help would be very welcome.
(sorry for being such a n00b)
November 14th, 2010, 07:50 PM
The yield :extra_header_tags tells Ruby to yield to that block when the page is parsed. What you want to do in each template is add the following:
<% content_for :extra_header_tags do %>
enter your custom title, etc here...
<% end %>
What Ruby will do is when it finds that content_for block it will inject it's contents into the matching yield mentioned above
November 24th, 2010, 10:59 AM
You could drop this in your layout.
<title><%= page_title %></title>
And then define the page title in a method in application_helper.rb
Or like this:
<title><%= @page_title %></title>
In which case, you'd set @page_title in every controller.