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? -- +S2 -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 254 bytes Desc: OpenPGP digital signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060104/4fea18b2/signature-0001.bin
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!
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'' %>
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 On 1/4/06, Simon Santoro <simon.santoro@poste.it> wrote:> > 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? > > > -- > +S2 > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060104/95bd0974/attachment.html
> <%= 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/ref_c_fixnum.html#Fixnum.size> <%= 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! > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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.> > I''d go with Chris Hall''s suggestion. > > cheers > Gerret > > [1] > http://www.mattriffle.com/mirrors/ruby_book/html/ref_c_fixnum.html#Fixnum.s >ize > > > <%= 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! > > > > _______________________________________________ > > 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!