Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5, "nil is not a valid asset source"

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.image_url(:thumb) %>

But after upgrading I get:

nil is not a valid asset source

Before upgrading to Rails 5, I didn't have any similar issue.

What could be at fault here? Can it be something else and not a Rails upgrade issue?

like image 797
Tasos Anesiadis Avatar asked May 30 '16 16:05

Tasos Anesiadis


2 Answers

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.

like image 145
Tasos Anesiadis Avatar answered Oct 20 '22 07:10

Tasos Anesiadis


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)

like image 43
Simranjit Singh Avatar answered Oct 20 '22 07:10

Simranjit Singh