Archive

Archive for July, 2009

Having some lunch

July 9th, 2009

Dynamic Title and Meta Tags using Ruby on Rails

July 9th, 2009

Here’s a quick tip on one way to do dynamic Title and/or Meta tags in your rails apps. It may not be the best way, but it works for us :-)

The problem is that you generally define your title and meta tags in your layout, but you may want to change the content in your templates depending on what page you are on. Here’s a quick way to keep it all in the view and do it using the content_for tag:

In your layout:

<title><%= (html_title = yield :html_title) ? html_title : 'My Default Title' %> - MySite.com</title>

<meta name="description" content="<%= (html_description = yield :html_description) ? html_description : 'My Default Description.' %>">

Then, in your template you can do something like this:

<% content_for :html_title, 'My Specific Page Title' %>
<% content_for :html_description, 'My Specific Page Description' %>

Hope that makes sense!

Gee RoR, Tips , , ,