34

I've looked on SO about how to add a <span> tag but I didn't see an example that placed the <span> where I want using Rails 3 link_to:

<a href="#" class="button white"><span id="span">My span&nbsp;</span>My data</a>

I tried something like:

<%= link_to(content_tag{:span => "My span&nbsp;", :id => "span"} @user.profile.my_data, "#", {:class => "button white"}) %>

But that didn't work.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
tvalent2
  • 4,959
  • 10
  • 45
  • 87

4 Answers4

88

link_to can take a block so I think you're after something like this:

<%= link_to '#', :class => 'button white' do %>
    <span id="span">My span&nbsp;</span><%= @user.profile.my_data %>
<% end %>
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • mu is too short, If you have a moment can you take a look at my follow-up to this. I want to add the value of the link you helped me produce to an array. http://stackoverflow.com/questions/7564247/adding-items-to-rails-3-array-onclick – tvalent2 Sep 27 '11 at 13:28
10

A combination of the .html_safe with #{@user.profile.my_data} should work as well.

<%= link_to "<span id='span'>My span&nbsp;</span>#{@user.profile.my_data}".html_safe, "#", class: 'button white' %>

You can also use a content_tag so it would look like:

<%= link_to(content_tag(:span, "My span&nbsp;", id:"span")+"#{@user.profile.my_data}", "#", class: 'button white' %> 

They're basically identical but one might be easier on the eyes for you. Also, I'm pretty new to coding so if this is dead wrong for some crazy reason, please just comment and I'll change it. Thanks.

Joe Susnick
  • 6,544
  • 5
  • 44
  • 50
  • 2
    This is not wrong, but `link_to ... do` was meant for exactly this and it looks much better than your solution IMO. – Mischa Feb 09 '14 at 02:59
5
link_to '#', :class => 'button white' do
  <span id="span">My span&nbsp;</span>My data
end
Arun Kumar Arjunan
  • 6,827
  • 32
  • 35
0

In HAML :

= link_to  new_post_mobile_path(topic.slug), class: 'add-new-place-btn' do
  %span{:class => "glyphicon glyphicon-plus", :style => "margin-right: 4px;"}
  New Place
mdev
  • 1,366
  • 17
  • 23