#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Location
    Toronto, Canada
    Posts
    5
    Rep Power
    0

    Rails: Modifying <head> to include various metas and title tags


    Hello,

    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:
    ...WEBSITE/stable/app/views/main

    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:

    PHP Code:
    <%= yield :extra_header_tags %>     
    <%= 
    stylesheet_link_tag 'jquery.lightbox-0.5' %>     
    <%= 
    stylesheet_link_tag 'style.css' %> 
        <%= 
    yield :stylesheets %>       
    <%= 
    javascript_include_tag ['jquery''jquery.lightbox-0.5''java.js'] %>     <%= yield :javascripts %>    
    <%= 
    yield %> 
    What is this <%= yield :extra_header_tags %>?
    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.
    Thanks!

    (sorry for being such a n00b)
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Title tag


    You could drop this in your layout.

    html Code:
    <title><%= page_title %></title>


    And then define the page title in a method in application_helper.rb

    Or like this:

    html Code:
    <title><%= @page_title %></title>


    In which case, you'd set @page_title in every controller.

IMN logo majestic logo threadwatch logo seochat tools logo