tva*_*nt2 7 paperclip ruby-on-rails-3
我正在使用Paperclip处理我的应用中的个人资料照片上传.他们上传得很好,并调整到我的模型中的规格.但是,如果用户的个人资料:照片为零,无论我尝试什么,我都无法更改默认值.这是我想要使用的代码:
<% if @profile.photo.nil? %>
<%= image_tag "public/images/example.jpg", :html => { :id => "noUserProfile" } %>
<% else %>
<%= image_tag @profile.photo.url(:normal) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了"../public/images/example.jpg",即使我的公共图片文件夹中有"example.jpg",也无法正常工作.当我在视图中复制图像地址时,我得到:
http://localhost:3000/photos/normal/missing.png
Run Code Online (Sandbox Code Playgroud)
我将这些文件夹添加到我的应用程序并在其中放入missing.png文件,没有任何内容.如果我转到上面的URL我得到No route matches "/photos/normal/missing.png"
有没有人对于发生了什么有任何想法?
配置文件模型中的has_attached_file:
has_attached_file :photo,
:styles => {
:normal => "153x220#",
:small => "75x108#" }
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这是:photo存在时呈现的HTML :
<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>
Run Code Online (Sandbox Code Playgroud)
这是在:photonil 时呈现的HTML :
<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>
Run Code Online (Sandbox Code Playgroud)
com*_*tic 15
我想你想要这个exists?方法.
if @profile.photo.exists?
Run Code Online (Sandbox Code Playgroud)
@profile.photo.nil?将永远是false因为它丢失时返回默认图像.
注意:这将检查真实文件是否存在,如果您在CDN上托管图像,则可能会非常慢.
作为一种解决方法,您可以只检查数据库是否认为存在文件:
if @profile.photo_file_name.present?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11706 次 |
| 最近记录: |