gpt4 book ai didi

ruby-on-rails - 我如何查看模型是否有带有 Carrierwave 的照片?

转载 作者:行者123 更新时间:2023-12-04 00:34:42 25 4
gpt4 key购买 nike

我有一个模型 - Post - 可以上传图片和文件。 Carrierwave 中的两个不同的 uploader 。

我想要做的是,对对象上是否存在照片上传进行简单检查,如果检测到,我将显示图像标签。但如果不是,则不会显示任何内容。

我试过这个:

<% if post.photo %>
<%= image_tag post.photo, size: "52", :class => 'entry-avatar' %>
<% end %>

但这不起作用,它总是显示:

<img class="entry-avatar" height="52" src width="52">

当它不为空时,它会将文件路径正确地添加到 src 属性中。

如果我尝试检查 photo.nil? 它不起作用,即使它看起来应该:

 > a 
=> #<Post id: 1, title: "Fire at Bible College in Christian", photo: nil, body: "A massive fire is now raging at the Bible College ...", created_at: "2014-08-28 08:06:19", updated_at: "2014-09-18 23:35:04", user_id: 1, ancestry: nil, file: nil, status: nil, slug: "fire-at-bible-college-in-christian", publication_status: 1, has_eyewitness: true>

请注意上面的 :photo 属性设置为 nil...但是当我这样做时:

 > a.photo
=> #<PhotoUploader:0x00000102e19878 @model=#<Post id: 1, title: "Fire at Bible College in Christian", photo: nil, body: "A massive fire is now raging at the Bible College ...", created_at: "2014-08-28 08:06:19", updated_at: "2014-09-18 23:35:04", user_id: 1, ancestry: nil, file: nil, status: nil, slug: "fire-at-bible-college-in-christian", publication_status: 1, has_eyewitness: true>, @mounted_as=:photo>
> a.photo.nil?
=> false

这是行不通的。

是否有一种 Carrierwave 方法可用于执行此操作或其他方式?

最佳答案

无需使用 nil? 方法检查 photo 属性。您可以像 a.photo? 一样直接检查它根据文件是否存在返回 bool 值。

关于ruby-on-rails - 我如何查看模型是否有带有 Carrierwave 的照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26049885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com