Forum: Ruby on Rails check if a file exists?

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.
B6e90ad1e69d563f1f13cccffe137262?d=identicon&s=25 Simon Santoro (Guest)
on 2006-01-04 11:57
(Received via mailing list)
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?
953c18019bceaa294ae0429bef6ab388?d=identicon&s=25 Norman Timmler (Guest)
on 2006-01-04 12:48
(Received via mailing list)
Am Mittwoch, den 04.01.2006, 11:57 +0100 schrieb Simon Santoro:
> 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' %>
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-01-04 13:15
(Received via mailing list)
File.file?(<filename>) will return true if file exists and it's a
regular
file (ie, not a directory, socket, etc), otherwise false

<%= image_tag @person_image
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-04 14:43
(Received via mailing list)
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 Santoro 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!
5ceea097a3b29cb6a5da6705926410f4?d=identicon&s=25 Gerret Apelt (Guest)
on 2006-01-04 18:14
(Received via mailing list)
> <%= 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 Hall's suggestion.

cheers
Gerret

[1]
http://www.mattriffle.com/mirrors/ruby_book/html/r...
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard (Guest)
on 2006-01-04 18:14
(Received via mailing list)
On Wednesday 04 January 2006 13:46, Gerret Apelt 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 .. ;-) .. 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 Santoro tried to type something
> >
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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!
This topic is locked and can not be replied to.