← Back to Upcase

Titles, Meta Tags and other per-page content variables

(Edward Loveall) #1

A tip / duh moment:

I had to add some custom meta tags in a site I’m building. They were different for every page. I considered putting them in a model somewhere, but that seemed like overkill since they weren’t being updated on any kind of regular basis. I also considered making different views that used other partials to fill out the content but that seemed messy.

I ended up creating an instance variable in my controller called @metas. It’s a hash that looks like this:

@metas = {
  description: "This is the meta description for the page",
  keywords: "foo, bar, baz, fizz, buzz"

I wrap that inside a private metas method in whatever controller I want to use:


def metas
  @metas = {

Then call a before filter on it:

before_filter :metas

Then, in my application layout, I have this block:

<% if @metas.present? %>
  <% @metas.each do |name, value| %>
    <meta name="<%= name %>" content="<%= value %>">
  <% end %>
<% end %>

Now, in any controller, I just have to define that metas method and set whatever parameters I want and it will pop up on that page.

There may be a better way to do this, but it seems to work pretty well. Would love to hear suggestions or comments if anyone has them.

(Pedro Moreira) #2

I would love to see this added to the Flutie gem…