首页 文章

Rails:如何设置og:image meta标签?

提问于
浏览
1

我的音乐评论应用程序为每个评论都有一个图像(专辑封面)(称为Pins) . 过去,当与Facebook分享评论时,专辑封面将显示为缩略图 . 现在根本没有图像显示 .

当我使用facebook debugger时,我收到以下信息:

og:图像未定义,无法下载或不够大 . 请使用og:image metatag定义所选图像,并使用至少200x200像素且可从Facebook访问的图像 . 将使用图像'http://s3.amazonaws.com/theTens/pins/images/000/000/150/medium/arcade-fire-reflektor-cover-500x500_(1).jpg?1387660087' .

我知道我需要设置一个类似于此的元标记:

<meta property="og:image" content='http://s3.amazonaws.com/theTens/pins/images/000/000/150/medium/arcade-fire-reflektor-cover-500x500_(1).jpg?1387660087'/>

如何设置元标记以根据用户共享的页面动态获取图像网址?

图像使用Paperclip上传,然后托管在Amazon S3上 .

编辑:下面是Prakash的正确答案,所以这里是我在应用程序助手中编写的辅助方法 . (我还必须设置url_path)

def og_image_path
  if @pin.present? 
    @pin.image.url
  end  
end  

def og_url_path
  if @pin.present? 
    "http://www.thetens.us/pins/" + @pin.to_param
  end  
end

我脑子里的meta标签:

<meta property="og:image" content="<%= og_image_path %>">
<meta property="og:url" content="<%= og_url_path %>">
<meta property="og:title" content="Album Review">
<meta property="og:description" content="The Music Review Site Where YOUR Opinion Matters">

1 回答

  • 1

    尝试在布局文件中添加以下内容:

    <meta property="og:image" content="<%= og_image_path %>">
    

    方法 og_image_path - 可以在 application_helper 中定义 - 如果存在则应提供评论图像的路径,如果不存在则应提供默认图像 .

    使用回形针提供的 image_file_name 属性来设置图像的值 . 有关详细信息,请参阅https://github.com/thoughtbot/paperclip#usage .

相关问题