问题描述
我刚刚升级到 Rails 5,但在尝试显示图像时遇到了一个奇怪的问题.
I have just upgraded to Rails 5 and I have a weird issue while trying to show an image.
我有 Rails 4 的确切代码:
I have the exact code I had for Rails 4:
<%= image_tag article.image_url(:thumb) %>
但升级后我得到:
nil is not a valid asset source
在升级到 Rails 5 之前,我没有遇到任何类似的问题.
Before upgrading to Rails 5, I didn't have any similar issue.
这里可能有什么问题?会不会是其他原因而不是 Rails 升级问题?
What could be at fault here? Can it be something else and not a Rails upgrade issue?
推荐答案
问题是我试图显示不存在的图像.
The problem was that I was trying to show an image that did not exist.
添加除非 article.image.blank?
解决了:
<%= image_tag article.image_url(:thumb) unless article.image.blank? %>
在 Rails 4 中,这只会在没有错误的情况下呈现任何内容,而在Rails 5 它引发了一个错误.所以这实际上是一个升级问题.
In Rails 4, this would have just rendered nothing without errors, while inRails 5 it raises an error. So it was, in fact, an upgrade issue.
非常感谢@BookOfGreg 指出这一点.
Big thanks to @BookOfGreg for pointing this out.
这篇关于Rails 5,“nil 不是有效的资产来源"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!