sean colquhoun
2006-Jun-06 06:15 UTC
[Rails] How do I wrap a <%= link to %> around an image?
I''ve got a bunch of dynamically generated link paths that I want to use to link some dynamically-generated image paths. Specifically, I want to link thumbnails in a gallery to the larger images. But I''m stymied by Rails'' syntax. I can''t figure out how to do this. I tried this: <%= link_to(image_tag("../../images/photos/photo.photogroup/TN_ photo.photogroup photo.filenumber .jpg"), "http://www.example.com") %> But of course, all of the variables are being translated into HTML literally instead of coughing up their values, so that''s no good. So I tried this: <%= link_to "something", {:action => ''show_album'', :album => @albumid, :photo => photo.filenumber} %> <img src="../../images/photos/<%= photo.photogroup %>/TN_<%= photo.photogroup %><%= photo.filenumber %>.jpg" alt="<%= photo.description %>"> Which works well, except that the best I can have with this is a text link NEXT to an image, when what I really want is the image to BE the link. Anybody know how to do this? TIA sean -- Posted via http://www.ruby-forum.com/.
Enclose the vars in #{}, e.g. <%= link_to(image_tag("../../images/photos/#{photo.photogroup}/TN_#{photo.photogroup}#{photo.filenumber}.jpg"), "http://www.example.com") %> Joe -- Posted via http://www.ruby-forum.com/.
Bharat Ahluwalia
2006-Jun-06 07:18 UTC
[Rails] RE:How do I wrap a <%= link to %> around an image?
> From: sean colquhoun <seancolquhoun@gk-a.com> > I''ve got a bunch of dynamically generated link paths that I > want to use > to link some dynamically-generated image paths. Specifically, > I want to > link thumbnails in a gallery to the larger images. > But I''m stymied by Rails'' syntax. I can''t figure out how to do this. > > I tried this: > > <%= link_to(image_tag("../../images/photos/photo.photogroup/TN_ > photo.photogroup photo.filenumber .jpg"), "http://www.example.com") %> > > But of course, all of the variables are being translated into HTML > literally instead of coughing up their values, so that''s no good. > > So I tried this: > > <%= link_to "something", {:action => ''show_album'', :album => > @albumid, > :photo => photo.filenumber} %> > <img src="../../images/photos/<%= photo.photogroup %>/TN_<%= > photo.photogroup %><%= photo.filenumber %>.jpg" alt="<%= > photo.description %>"> > > Which works well, except that the best I can have with this is a text > link NEXT to an image, when what I really want is the image to BE the > link.Well from the code sample it looks like u need some string formatting. Try doing <%= link_to(image_tag("../../images/photos/#{photo.photogroup}/TN_ #{photo.photogroup) #{photo.filenumber}.jpg"), "http://www.example.com") %> This should see u through. Bharat
sean colquhoun
2006-Jun-06 07:49 UTC
[Rails] Re: How do I wrap a <%= link to %> around an image?
Thanks Joe! That worked great for the variables. Unfortunately, I still can''t link to functions ? only static URIs. Rails keeps throwing me errors when I try to do something like this: <%= link_to (image_tag("../../images/photos/#{photo.photogroup}/TN_#{photo.photogroup}#{photo.filenumber}.jpg"), {:action => ''show_album'', :album => @albumid, :photo => photo.filenumber} %> Is my syntax off or is this just something you can''t do? -- Posted via http://www.ruby-forum.com/.
sean colquhoun wrote:> Thanks Joe! That worked great for the variables. Unfortunately, I still > can''t link to functions ? only static URIs. Rails keeps throwing me > errors when I try to do something like this: > > <%= link_to > (image_tag("../../images/photos/#{photo.photogroup}/TN_#{photo.photogroup}#{photo.filenumber}.jpg"), > {:action => ''show_album'', :album => @albumid, :photo => > photo.filenumber} %> > > Is my syntax off or is this just something you can''t do?What errors - like ''missing parenthesis''? Your code is missing a closing one at the end, for link_to. Joe -- Posted via http://www.ruby-forum.com/.
sean colquhoun
2006-Jun-06 08:05 UTC
[Rails] Re: How do I wrap a <%= link to %> around an image?
WOOHOO! Thanks Joe! -- Posted via http://www.ruby-forum.com/.
Enrico Franchi
2006-Jun-06 09:00 UTC
[Rails] How do I wrap a <%= link to %> around an image?
On Tue, 6 Jun 2006 08:14:51 +0200, sean colquhoun wrote:> Anybody know how to do this?I suppose I did not understand your problem. image_tag(source, options = {}) Returns an image tag converting the options into html options on the tag, but with these special cases: <snip> The src can be supplied as a? ? full path, like "/my_images/image.gif" ? file name, like "rss.gif", that gets expanded to "/images/rss.gif" ? file name without extension, like "logo", that gets expanded to "/images/logo.png" You can pass image_tag a full path. That is a ruby string... so you can do everything you would do with ruby strings (and in particular use the #{} ). Combining this you should do something like: link_to ( image_tag ("../../images/photos/#{photo.photogroup}"/TN_#{photo.photogroup}#{photo.filenumber}.jpg"), {:action => ''show_album'', :album => @albumid, :photo => photo.filenumber} )
sean colquhoun
2006-Jun-06 09:20 UTC
[Rails] Re: How do I wrap a <%= link to %> around an image?
Thanks Enrico! That''s pretty much what I ended up doing. My difficulty, since I''m still quite the novice at this (and I expect I will be for some time) was more from having to guess at how to combine the two items, since there was no example of something like that in the API. -- Posted via http://www.ruby-forum.com/.
Enrico Franchi
2006-Jun-06 09:28 UTC
[Rails] Re: How do I wrap a <%= link to %> around an image?
On Tue, 6 Jun 2006 11:20:46 +0200, sean colquhoun wrote:> Thanks Enrico! That''s pretty much what I ended up doing. My difficulty, > since I''m still quite the novice at this (and I expect I will be for > some time) was more from having to guess at how to combine the two > items, since there was no example of something like that in the API.Well.. I didn''t notice a solution had already been posted. I''d suggest you should also consider learning some ruby as a language. Many things become clearer if you know ruby.