Kris Leech
2005-Nov-25 15:51 UTC
how to prevent image_tag helper throwing exception if the url is null or blank?
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.
Derek Chesterfield
2005-Nov-25 17:30 UTC
Re: how to prevent image_tag helper throwing exception if the url is null or blank?
Does this work? <%= image_tag(@product.image) if @product.image %> On 25 Nov 2005, at 3:51pm, 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.
Ezra Zygmuntowicz
2005-Nov-26 17:08 UTC
Re: how to prevent image_tag helper throwing exception if the url is null or blank?
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-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Chris Hall
2005-Nov-26 17:33 UTC
Re: how to prevent image_tag helper throwing exception if the url is null or blank?
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
On 11/25/05, Kris Leech
<krisleech-BSIDdvZawMx9qp0gCGiW7Q@public.gmane.org>
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.
>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails