How do I wrap a <%= link to %> around an image?


#1

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} %>
<%= 
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


#2

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


#3

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?


#4

WOOHOO!

Thanks Joe!


#5

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


#6

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.


#7

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:

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}
)


#8

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.