# Image\_tag problem

**URL:** <https://www.ruby-forum.com/t/image-tag-problem/70337>\
**Category:** Rails\
**Created:** [September 28, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/image-tag-problem/70337 "2006-09-28T12:01:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 28, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/image-tag-problem/70337/1 "2006-09-28T12:01:35Z")

</div>

Hi

I am very new to RoR and am learning using the Agile Web D.  
with Rails referece. I am slowly creating the Depot application and  
have a problem setting up an image to act as a hyper link.

The code snippt I specifically have a problem with is

\<%= link\_to image\_tag(’\<%= product.image\_url %\>’), :action =\>  
:add\_to\_cart, :id =\> product %\>

If I replace \<%= product.image\_url %\> with static text e.g.  
/images/17.jpg the line of code works but I need to change the image  
for each db record.

I have spent hours searching the web to help point me in the right  
direction to no avail.

Thanks in advance for your help

Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 28, 2006, 12:25pm UTC](https://www.ruby-forum.com/t/image-tag-problem/70337/2 "2006-09-28T12:25:36Z")

</div>

> The code snippt I specifically have a problem with is
> 
> \<%= link\_to image\_tag(’\<%= product.image\_url %\>’), :action =\>  
> :add\_to\_cart, :id =\> product %\>

This code looks a bit wrong… You don’t need to use the output tags  
(\<%= %\>) in that string. In fact, it shouldn’t be a string at all in  
this case. This should work fine:

\<%= link\_to image\_tag(product.image\_url), :action =\> :add\_to\_cart, :id  
=\> product %\>

Hope that helps,

Steve

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 28, 2006, 12:25pm UTC](https://www.ruby-forum.com/t/image-tag-problem/70337/3 "2006-09-28T12:25:37Z")

</div>

On 9/28/06, Mark [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= link\_to image\_tag(‘\<%= product.image\_url %\>’), :action =\>  
> :add\_to\_cart, :id =\> product %\>

Inside the \<%= and %\> you are using ruby code. What you have  
effectively  
done here is tried to firstly put a string literal into the image\_tag  
method. The you’ve used an erb escape inside ruby script. The good  
news  
is you don’t need to do this 🙂

I imagine that if you looked at your source you would find something  
like  
[![]()](https://www.ruby-forum.com/add_to_cart/2)

Instead use  
\<%= link\_to image\_tag( product.image\_url ), :action =\> :add\_to\_cart, :id  
=\>  
product %\>

That should resolve the image url to the product objects image\_url  
method.

Hope that helps a bit.

Cheers  
Dan

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 28, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/image-tag-problem/70337/4 "2006-09-28T12:49:38Z")

</div>

Guys

Thank you for taking the time to reply, much appreciated.

Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [September 28, 2006, 12:37pm UTC](https://www.ruby-forum.com/t/image-tag-problem/70337/5 "2006-09-28T12:37:37Z")

</div>

On 28 September 2006 13:56, Mark wrote:

> \<%= link\_to image\_tag(’\<%= product.image\_url %\>’), :action  
> =\> :add\_to\_cart, :id =\> product %\>
> 
> If I replace \<%= product.image\_url %\> with static text e.g.  
> /images/17.jpg the line of code works but I need to change the image  
> for each db record.

The line should be as follows:

\<%= link\_to image\_tag(product.image\_url), :action =\> :add\_to\_cart, :id  
=\>  
product %\>

You see, you have already opened \<%= %\> braces, so another pair will not  
be  
evaluated. Also, there is no need for such complex transformations.
