How to prevent image_tag helper throwing exception if the ur


#1

If I store the path/filename of an image in the database how do I
prevent “image_tag” helper from throwing an exception if a filename/path
has not been set?

<%= image_tag @product.image %>
or
<%= image_tag url_for_file_column(“product”, “image”) %>

Many thanks, K.


#2

Does this work?

<%= image_tag(@product.image) if @product.image %>


#3

On Nov 25, 2005, at 7:51 AM, Kris L. wrote:

If I store the path/filename of an image in the database how do I
prevent “image_tag” helper from throwing an exception if a filename/
path has not been set?

<%= image_tag @product.image %>
or
<%= image_tag url_for_file_column(“product”, “image”) %>

Many thanks, K.

Kris-

You can use this:

<%= image_tag @product.image rescue nil %>

<%= image_tag url_for_file_column(“product”, “image”) rescue nil %>

Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#4

i might do something like so…

<%= image_tag product.get_image, :border => 0 %>

class Product < ActiveRecord:Base

returns either the image (path/name stored in db)

or default image name if image is nil (null in db)

def get_image
# default_product_image.jpg in public/images directory
(image.nil?) ? “default_product_image.jpg” : image
end
end