【发布时间】:2012-10-27 16:35:10
【问题描述】:
当我上传图片然后显示它时,Paperclip 工作正常:
<%= image_tag @post.photo.url(:medium) %>
问题是,如果图片没有上传,我会得到这个错误:
NoMethodError in Posts#show
Showing /Users/me/RubymineProjects/level_60/app/views/posts/show.html.erb where line #16 raised:
undefined method `[]' for nil:NilClass
我尝试使用<% if @post.photo %> 或<% if @post.photo != '' %> 检查图像是否存在。但是,即使没有上传图片,这两者也总是返回 true。
只有在回形针存在的情况下,如何才能通过回形针显示图像?
【问题讨论】:
-
试试
<% if @post.photo.any? %> -
这是我尝试时得到的错误:
undefined methodany?'对于#<:attachment:0x007ffa86e4be38>`
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 paperclip paperclip-validation