MikeHolford
MikeHolford

Reputation: 1931

og:image meta tag in ruby on rails

I have some dynamic images defined as follows on my page:

<%= image_tag(@recipe.image.url(:fixed), :class => 'recipe_image_show') %>

I am looking for a way to define og:image with this code. The og:description code seen below works perfectly:

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

But if I use the code below then it doesn't want to work:

<meta property="og:image" content='<%= image_tag(@recipe.image.url(:fixed), :class => 'recipe_image_show') %>'/>

Any suggestions? I realised I have used single quotes here but using double quotes for some reason results the page to disregard the '/>' part of the meta tag and display it as text.

Upvotes: 6

Views: 7156

Answers (3)

OuttaSpaceTime
OuttaSpaceTime

Reputation: 976

if you are on rails 6 with wepback this one worked for me

- meta 'og:image': "#{asset_pack_url "static/image.webp"}"

check asset_pack_url on github for reference

your image path may vary, lookup manifest.json for this manner

Upvotes: 1

Cristian Rennella
Cristian Rennella

Reputation: 727

Use the tool https://developers.facebook.com/tools/debug/

Click "Scrape Again" to reload Facebook Cache

Use full path with image_url("example.png")

Upvotes: 0

Kirti Thorat
Kirti Thorat

Reputation: 53038

As per the chat discussion, OP is hosting the images on S3.

So, define the meta tag as below:

<meta property="og:image" content='<%= @recipe.image.url(:fixed) %>'/>

This will be evaluated to:

<meta property="og:image" content='https://s3....amazonaws.com/path/to/image.jpg'/>

NOTE:

If image was stored on the your own application server then you could use it as:

<meta property="og:image" content='<%= image_url @recipe.image.url(:fixed) %>'/>

Upvotes: 6

Related Questions