Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding span tag in Rails link_to

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.

like image 461
tvalent2 Avatar asked Sep 27 '11 03:09

tvalent2


2 Answers

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 %>
like image 72
mu is too short Avatar answered Nov 09 '22 21:11

mu is too short


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.

like image 10
Joe Susnick Avatar answered Nov 09 '22 21:11

Joe Susnick