Forum: Ruby on Rails How do I wrap a <%= link to %> around an image?

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.
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2006-06-06 08:14
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
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-06-06 09:12
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
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2006-06-06 09:49
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?
429500a5a54600958c9c7ac032a37f66?d=identicon&s=25 Joe (Guest)
on 2006-06-06 09:57
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
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2006-06-06 10:05
WOOHOO!

Thanks Joe!
47d626d416d9da060a44ed9f684e32cf?d=identicon&s=25 Enrico Franchi (rik0)
on 2006-06-06 11:03
(Received via mailing list)
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}
	)
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2006-06-06 11:20
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.
47d626d416d9da060a44ed9f684e32cf?d=identicon&s=25 Enrico Franchi (rik0)
on 2006-06-06 11:31
(Received via mailing list)
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.
This topic is locked and can not be replied to.