Check if a file exists?

Hello all!
I have a list of people on a page, and some of this people have pictures
of them stored on the web server. So I have an image tag like this on my
page:
<%= image_tag “/images/people/” + person.pers_id.to_s + “.jpg” %>
But not all have a picture, so if the file for pers_id=1899 does not
exist, i would like to display a custom image for that guy.
How can I do that?

Am Mittwoch, den 04.01.2006, 11:57 +0100 schrieb Simon S.:

Hello all!
I have a list of people on a page, and some of this people have pictures
of them stored on the web server. So I have an image tag like this on my
page:
<%= image_tag “/images/people/” + person.pers_id.to_s + “.jpg” %>
But not all have a picture, so if the file for pers_id=1899 does not
exist, i would like to display a custom image for that guy.
How can I do that?

<%= File.exists?(RAILS_ROOT +
“/public/images/people/#{person.pers_id}.jpg”) ?
“/images/people/#{person.pers_id}.jpg” :
‘/images/people/custom_image.jpg’ %>

File.file?() will return true if file exists and it’s a
regular
file (ie, not a directory, socket, etc), otherwise false

<%= image_tag @person_image

<%= if @person.pers_id.size > 0 %>

You should use <% if foo %> rather than <%= if foo %>.

pers_id is likely a Fixnum, certainly not a File object. Calling
‘size’ on it won’t return the filesize that the OP is looking to
figure. Fixnum#size “Returns the number of bytes in the machine
representation of a Fixnum.” [1]

I’d go with Chris H.'s suggestion.

cheers
Gerret

[1]
http://www.mattriffle.com/mirrors/ruby_book/html/ref_c_fixnum.html#Fixnum.size

Simon,

Something like this should work. (check the syntax because I’m a newbie)

<%= if @person.pers_id.size > 0 %>
<%= image_tag “/images/people/” + person.pers_id.to_s + “.jpg” %>
<% else %>
#Not sure on this syntax!
<%= image_tag “/images/people/” + person.defaultpic + “.jpg” %>
<% end %>

Regards,

Gerard.

On Wednesday 04 January 2006 11:57, Simon S. tried to type
something
like:

Hello all!
I have a list of people on a page, and some of this people have pictures
of them stored on the web server. So I have an image tag like this on my
page:
<%= image_tag “/images/people/” + person.pers_id.to_s + “.jpg” %>
But not all have a picture, so if the file for pers_id=1899 does not
exist, i would like to display a custom image for that guy.
How can I do that?


“Who cares if it doesn’t do anything? It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;
~
:wq!

On Wednesday 04 January 2006 13:46, Gerret A. tried to type something
like:

<%= if @person.pers_id.size > 0 %>

You should use <% if foo %> rather than <%= if foo %>.
Clear thanx!

pers_id is likely a Fixnum, certainly not a File object. Calling
‘size’ on it won’t return the filesize that the OP is looking to
figure. Fixnum#size “Returns the number of bytes in the machine
representation of a Fixnum.” [1]
I was asumming (right, I know, that’s the problem … :wink: … That if a
persons
id number was not set that a default picture should be called. In this
case
the field ( if not NULL) should return > 0. That should work though
wouldn’t
it?

But I get it (now) the id is known, just not if a picture is available.

Sorry about the not so proper syntaxed advise, but I’m a newbie on my
way up.

Regards,

Gerard.

    <%= image_tag "/images/people/" + person.pers_id.to_s + ".jpg" %>

On Wednesday 04 January 2006 11:57, Simon S. tried to type something

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


“Who cares if it doesn’t do anything? It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;
~
:wq!