无法弄清楚如何为回形针配置默认配置文件图像

时间:2016-05-01 20:37:59

标签: ruby-on-rails-4 paperclip

我正在阅读有关此问题的Stack帖子,但我仍未成功获取默认个人资料图片。

在我的app / models文件夹中,我有一个名为" profile.rb"的文件。看起来像这样:

class Profile < ActiveRecord::Base
  belongs_to :user

  # From Paperclip GitHub README
  has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.jpg"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end

据我所知,它基本上说风格是某种占位符,适合中等和拇指。所以&#34;拇指&#34;和&#34;媒体&#34;应该是资产/图像内的文件夹。 default_url表示将在以下位置找到默认图像:&#34; thumb&#34;或&#34;媒体&#34;夹。我创建了一个名为&#34; thumb&#34;在我的images目录中,然后放置&#34; missing.jpg&#34;文件在里面。在我的show.html.erb中,我有一行

<%= image_tag @user.profile.avatar.url %>

用于在用户上传图像时显示图像,但显示为&#34;缺少&#34;如果他们没有上传图片。

我还注意到在public / profiles / avatars / 000/000/004目录中有3个名为&#34; medium,&#34; &#34;原始,&#34;和&#34;拇指。&#34;我认为这是我需要放置我的&#34; missing.jpg,&#34;但这也不起作用,所以我对该怎么做有点困惑。

根据this stack post,Paperclip正在寻找公共目录中的图像,所以我假设这是正确的地方,但我仍然不明白它为什么不能工作。

1 个答案:

答案 0 :(得分:0)

您丢失的图片必须位于公共文件夹中,或者您可以将其保存在 app / assets / images 文件夹中,因为它必须全局可用,并且如果您将来放弃您的头像表,丢失的图像将消失。

将丢失的图像保存在图像文件夹中。

您可以将default_url指定为:

default_url: "/assets/icon/missing.jpg"

或者您可以在视图中添加条件

<% if @user.avatar? %>
    <%= image_tag @user.profile.avatar.url(:medium)  %>    
<% else %>
    <%= image_tag "/assets/icon/missing.jpg" %>
<% end %>
相关问题