How do I add a span in a link_to with an image

Matt Elhotiby picture Matt Elhotiby · Jan 14, 2011 · Viewed 9.4k times · Source

So I have this:

<%= link_to(image_tag(@model.picture.url(:thumb), :alt => ''), "/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) %>

Which works, but I need a span in between also like this:

 <a id="y_link_2" href="/pages/you/2" class="">
     <span>Apples</span>
     <img src="another_small.jpg?1236340989" alt="">
 </a>

How do I add

 <span>Apples</span>

to the link_to?

Answer

Shaun picture Shaun · Jan 14, 2011

Feed a block to your link_to call:

<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
  <%= content_tag(:span, 'Apples') %>
  <%= image_tag(@model.picture.url(:thumb), :alt => '') %>
<% end %>

Alternatively:

<% link_to("/pages/you/#{something.id}", {:id => "y_link_#{something.id}"}) do %>
  <span>Apples</span>
  <%= image_tag(@model.picture.url(:thumb), :alt => '') %>
<% end %>