在 Paperclip 中指定 missing.png [英] Specifying missing.png in Paperclip

查看:42
本文介绍了在 Paperclip 中指定 missing.png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Paperclip 处理我的应用程序中的个人资料照片上传.他们上传得很好并调整到我模型中的规格.但是,如果用户的个人资料 :photo 为零,无论我尝试什么,我都无法更改默认设置.这是我要使用的代码:

<% 如果@profile.photo.nil?%><%= image_tag "public/images/example.jpg", :html =>{ :id =>"noUserProfile" } %><%其他%><%= image_tag @profile.photo.url(:normal) %><%结束%>

我已经尝试过../public/images/example.jpg",但即使我的公共图片文件夹中有example.jpg",这也不起作用.当我在视图中复制图片地址时,我得到:

http://localhost:3000/photos/normal/missing.png

我将这些文件夹添加到我的应用程序中,并在其中放置了一个 missing.png 文件,但什么也没有.如果我转到上面的 URL,我得到 No route matching "/photos/normal/missing.png"

有人知道发生了什么吗?

配置文件模型中的

has_attached_file:

has_attached_file :photo,:styles =>{:正常=>"153x220#",:小 =>"75x108#" }

add_attachment_photo_to_profile 迁移:

class AddAttachmentPhotoToProfile <ActiveRecord::迁移def self.upadd_column :profiles, :photo_file_name, :stringadd_column :profiles, :photo_content_type, :stringadd_column :profiles, :photo_file_size, :integeradd_column :profiles, :photo_updated_at, :datetime结尾def self.downremove_column :profiles, :photo_file_nameremove_column :profiles, :photo_content_typeremove_column :profiles, :photo_file_sizeremove_column :profiles, :photo_updated_at结尾结尾

这是在 :photo 存在时呈现的 HTML:

<div class="smFrame"><div class="smUserPhoto"><img alt="8217_667699353137_15600054_38423586_7789442_n" src="/system/photos/1/small/8217_667699353137_15600054_36842054_3842053_3684253137_868427589584275895842735894427538894427538894427789427894278942788427894588942778942778942778942789442782张照片

<div class="findinfo"><p><a href="/profiles/1">姓名</a></p>

这是当 :photo 为零时呈现的 HTML:

<div class="smFrame"><div class="smUserPhoto"><img alt="Missing" src="/photos/small/missing.png"/>

<div class="findinfo"><p><a href="/profiles/2">姓名</a></p>

解决方案

查看 rdocs,这就是它按原样呈现的原因.没有附件时,Paperclip 会处理这种情况.

您可以将默认值设置为不同的内容,避免在模板中进行额外工作.

I am using Paperclip to handle profile photo uploads in my app. They upload well and resize to the specifications in my model. However, if a User's profile :photo is nil, no matter what I try I can't change the default. Here's the code I want to use:

<% if @profile.photo.nil? %>
<%= image_tag "public/images/example.jpg", :html => { :id => "noUserProfile" } %>
<% else %>
<%= image_tag @profile.photo.url(:normal) %>
<% end %>

I've tried "../public/images/example.jpg" and that doesn't work either even though I have "example.jpg" in my public images folder. When I copy image address in my view, I get:

http://localhost:3000/photos/normal/missing.png

I added those folders to my app and put a missing.png file in there and nothing. If I go to the above URL I get No route matches "/photos/normal/missing.png"

Does anyone have any ideas as to what's going on?

has_attached_file in Profile model:

has_attached_file :photo,
  :styles => {
  :normal => "153x220#",
  :small => "75x108#" }

add_attachment_photo_to_profile migration:

class AddAttachmentPhotoToProfile < ActiveRecord::Migration
  def self.up
    add_column :profiles, :photo_file_name, :string
    add_column :profiles, :photo_content_type, :string
    add_column :profiles, :photo_file_size, :integer
    add_column :profiles, :photo_updated_at, :datetime
  end

  def self.down
    remove_column :profiles, :photo_file_name
    remove_column :profiles, :photo_content_type
    remove_column :profiles, :photo_file_size
    remove_column :profiles, :photo_updated_at
  end
end

This is the HTML that's rendered for when the :photo exists:

<div class="userSnapshot">
  <div class="smFrame">
    <div class="smUserPhoto">
      <img alt="8217_667699353137_15600054_38423586_7789442_n" src="/system/photos/1/small/8217_667699353137_15600054_38423586_7789442_n.jpg?1316052048" />
    </div>
  </div>
  <div class="findinfo">
    <p><a href="/profiles/1">Name</a></p>
  </div>
</div>

This is the HTML that's rendered for when the :photo is nil:

<div class="userSnapshot">
  <div class="smFrame">
    <div class="smUserPhoto">
      <img alt="Missing" src="/photos/small/missing.png" />
    </div>
  </div>
  <div class="findinfo">
    <p><a href="/profiles/2">Name</a></p>
  </div>
</div>

解决方案

Check out the :default_url option in the rdocs, that's why it's rendering as it is. Paperclip handles the cases when there's no attached file.

You can set the default to something different and avoid the extra work in your template.

这篇关于在 Paperclip 中指定 missing.png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆