Specifying missing.png in the folder

I use Paperclip to process photo photos in the application. They load and resize well to specifications in my model. However, if the user profile: photo is zero, no matter what I try, I cannot change the default value. Here is 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 tried "../public/images/example.jpg" and this does not work even if I have "example.jpg" in my public image folder. When I copy the image address in my opinion, I get:

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

I added these folders to my application and put the missing.png file 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 is going on?

has_attached_file in the profile model:

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

add_attachment_photo_to_profile :

 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 displays when it exists :photo :

 <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 displays when :photo is zero:

 <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> 
+7
source share
2 answers

Pay attention to the parameter :default_url in rdocs , therefore its rendering as is. Paperclip handles cases where there is no attachment.

You can set something else by default and avoid the extra work in your template.

+11
source

I think you need an exists? method exists? .

 if @profile.photo.exists? 

@profile.photo.nil? will always be false because it returns the default image when it is missing.


NOTE This checks if the file really exists, and can be very slow if you place your images on a CDN.

As work around, you can simply check if the database is thinking about the file:

 if @profile.photo_file_name.present? 
+15
source

All Articles