rails 3 link_to with nested content_tag to create <a href with nested span - how to?

user1260945 picture user1260945 · Mar 10, 2012 · Viewed 18.8k times · Source

Hi I got a noob question, I want to create the following HTML result:

<a href="/controller/action" class="button-big layer">TEXT<span class="arrow-big"></span></a>

In the above HTML I want to have text with a span-class to style in an image via css.

When I try the following implementations the result reflects just one part of the needed implementation:

<%= link_to "TEXT", controller_path, :class => "button-big layer" %>

results in:

<a href="/controller/action" class="button-big layer">TEXT</a>

and

<%= link_to(content_tag(:span, "", :class => "arrow-big"), controller_path, :class => "button-big layer") %>

results in:

<a href="/controller/action" class="button-big layer"><span class="arrow-big"></span></a>

Does anyone know how to accomplish?

Answer

Johny picture Johny · Mar 10, 2012

You could also nest tags by using alternative syntax for link_to helper

<%= link_to controller_path, :class=> "button-big layer" do %>
  Text
  <%= content_tag(:span, "", :class => "arrow_big" %>
<% end %>