Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook OG Meta Tags in application.html.erb

In my rails application, I want to connect the FB OG Meta Tags dynamically with the page that the user is currently on. I feel like this should be straightforward, but can't get it to work properly.

For example, I have "Idea" Pages, on which there is an Idea title and an Idea Description.

Currently, in my Application.html.erb file I have this (and am getting a NoMethodError for title):

<meta property="og:title" content= <%= "#{@idea.title.titleize}" %>/>
<meta property="og:url" content= <%= "http://myurl.com/ideas/#{@idea.id}" %>/>
<meta property="og:description" content= <%= "#{@idea.short_description}" %>/>

Is there an issue with my syntax? With the availability of these instance variables? I'm not sure I totally understand how this works. Thanks!

like image 856
sacshu Avatar asked Nov 30 '25 09:11

sacshu


2 Answers

Yes as noob mentioned, make the content declaration be within quotes.

<meta property="og:description" content= "<%= @idea.short_description %>" />

To make these meta tags dynamic in Rails, people first started using the pretty page title method described by Ryan Bates in the pretty page title railscast

However, I just came across this meta-tags gem which looks extremely promising: meta-tags

With the gem, manage any meta tag, make them dynamic and SEO friendly. Example:

set_meta_tags :title => 'Member Login'
# <title>Some Page Title</title>

Easily set all facebook:og tags at once:

  set_meta_tags :og => {
                  :title    => 'The Rock',
                  :type     => 'video.movie',
                  :url      => 'http://www.imdb.com/title/tt0117500/',
                  :image    => 'http://ia.media-imdb.com/rock.jpg',
                  :video    => {
                    :director => 'http://www.imdb.com/name/nm0000881/',
                    :writer   => ['http://www.imdb.com/name/nm0918711/', 'http://www.imdb.com/name/nm0177018/']
                  }
                }

The above code outputs the following html when you then call <%=display_meta_tags%>:

<meta property="og:title" content="The Rock"/> <meta property="og:type" content="video.movie"/> <meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/> <meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/> <meta property="og:video:director" content="http://www.imdb.com/name/nm0000881/"/> <meta property="og:video:writer" content="http://www.imdb.com/name/nm0918711/"/> <meta property="og:video:writer" content="http://www.imdb.com/name/nm0177018/"/>

Many more examples in the source code and github repo. The last tag set takes precedence, so if you set one in your layout, then another in your page, seems like the one in the page will take precedence.

If you use this, just make sure you have <%= display_meta_tags %> in your layout page i.e. application.html.erb.

like image 138
Danny Avatar answered Dec 02 '25 04:12

Danny


I believe the correct syntax is:

<meta property="og:description" content= "<%= @idea.short_description %>" />
like image 26
noob Avatar answered Dec 02 '25 03:12

noob