Forum: Ruby on Rails how to prevent image_tag helper throwing exception if the ur

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Kris Leech (krisleech)
on 2005-11-25 16:55
(Received via mailing list)
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.
B4a3fe24e9fb82df573ae5b38aa3b872?d=identicon&s=25 dez (Guest)
on 2005-11-25 18:32
(Received via mailing list)
Does this work?

<%= image_tag(@product.image) if @product.image %>
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 ezra (Guest)
on 2005-11-26 18:10
(Received via mailing list)
On Nov 25, 2005, at 7:51 AM, Kris Leech 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 Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 christopher.k.hall (Guest)
on 2005-11-26 18:35
(Received via mailing list)
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
This topic is locked and can not be replied to.