Reputation: 1796
I am using Paperclip to store profile pictures for users. I am trying to be quite strict, by allowing only certain file types, etc. Nevertheless it is quite common for users to manage to mishandle something.
In my index.html.erb file I list all user profiles, but it is common for this list to throw Application Error on the production environment, if one of the images is damaged, cannot be found etc, the whole thing fails, and the whole page fails.
Currently I find the image like this
<% @members.each do |member| %>
<tr>
<td><%= image_tag member.avatar.url(:thumb) %></td>
</tr>
<% end %>
How do I wrap this in a code, that would not allow the page to fail, if for some reason the image fails, and would display an alternative image, as specified in PaperClip documentation (aka default image).
Upvotes: 2
Views: 597
Reputation: 1074
Try this.
In your model:
has_attached_file :image, :default_url => "/images/no-image.jpg"
Upvotes: 2
Reputation: 3011
I would suggest to write a method in model that checks the condition and call the method from View.
Model:
def image_url(image_size)
avatar_image = member.avatar
if avatar_image.present? && avatar_image.url(image_size).present?
return avatar_image.url(image_size)
else
return "/images/default_image.jpg"
end
end
View:
<%= image_tag member.image_url(:thumb) %>
Upvotes: 4