Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ActionView::Template::Error ( isn't precompiled)" raised on "image_tag nil"

If I don't handle view correctly, Production environment show 500.

<%= image_tag post.user.image_url %>

This could be

<%= image_tag post.user.image_url if post.user && post.user.image_url %>

but I am little careless and forgot this issue several times.

How can I prevent this? How can I use <%= image_tag nil %> in production environment without raising 500?

like image 904
ruseel Avatar asked Apr 09 '12 08:04

ruseel


1 Answers

image_tag must have a source, Rails can do nothing with it, but raise an exception.

You can write a helper like this:

module ApplicationHelper
  def safe_image_tag(source, options = {})
    source ||= "default.jpg"
    image_tag(source, options)
  end
end

or simply check for nil directly in a view. Anyway you have to do something to prevent an error.

like image 168
Nash Bridges Avatar answered Sep 26 '22 00:09

Nash Bridges