Rails 5, “nil is not a valid asset source”

后端 未结 4 2038
暗喜
暗喜 2021-02-12 09:56

I have just upgraded to Rails 5 and I have a weird issue while trying to show an image.

I have the exact code I had for Rails 4:

<%= image_tag article         


        
相关标签:
4条回答
  • 2021-02-12 10:07

    I don't know this is compact solution or not but this code will work.

    activate the fallback method in your uploader.

      def default_url
        "/assets/fallback/" + [version_name, "default.png"].compact.join('_')
      end
    

    Hope this will help you.

    Cheers (y)

    0 讨论(0)
  • 2021-02-12 10:10

    The problem was that I was trying to show an image that did not exist.

    Adding unless article.image.blank? solved it:

    <%= image_tag article.image_url(:thumb) unless article.image.blank? %>
    

    EDIT: In Rails 4, this would have just rendered nothing without errors, while in Rails 5 it raises an error. So it was, in fact, an upgrade issue.

    Big thanks to @BookOfGreg for pointing this out.

    0 讨论(0)
  • 2021-02-12 10:25

    Try with this, you need to add unless condition in your code. You need to add below code

    <%= image_tag article.image_url(:thumb) unless article.image.blank? %>

    0 讨论(0)
  • 2021-02-12 10:29

    I hope this code snippet will help those future readers.

    <td><%= image_tag image.picture.url, size: "100x100" unless image.picture.url.blank? %></td>
    

    without that unless image.picture.url.blank? part of the code,

    "nil is not a valid asset source"

    shows up when uploading empty image.

    0 讨论(0)
提交回复
热议问题