开发者

Only display a user-uploaded photo if it exists in rails

<%= image_tag message.photo.url(:small) %>

I have paperclip installed and the above code displays an image that the user uploads which has been resized to :small (in this case 40x40px. How can I get my page to display this image only if it exists? Currently if the message includes a photo that photo is displayed but all other messages show broken image lin开发者_Python百科ks. Thanks


Paperclip adds the name of the attachment suffixed with a "?" to the attached model as a helper method to allow you to see whether or not there is an attachment. In your case, the helper method would be photo? on the message class. You could use it with the tertiary operator in this manner:

<%= message.photo? ? image_tag message.photo.url(:small) : "" %>

Or, if you'd like to show a default no-image image when there is no image...

<%= image_tag message.photo? ? message.photo.url(:small) : url_to_no-image_image %>


It's been a while since I've used rails, but wouldn't it suffice to just put in an if statement?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜