Ruby on Rails seems to be auto-escaping html created by link_to

Here is my code, I'm trying to display a list of links to a bboy's crews in sentence form with .to_sentence

<span class="affiliation">
    <% if(@bboy.crews.count > 0)%>
    <span><%= if(@bboy.crews.size > 1) then "Crew".pluralize else "Crew" end %>:</span>
    <%= @bboy.crews.collect{|c| link_to, c}.to_sentence %>
    <% else %>  
    <% end %>

The output I get is the correct links but it displays as:

<a href="/crews/1" rel="nofollow">Hustle Kidz</a> and <a href="/crews/2" rel="nofollow">Knuckleheads Cali</a>

rather than:

Hustle Kidz and Knuckleheads Cali

with the html escaped, rather than the desired links.

Am I missing something? I've tried CGI.unescapeHTML and a few others but am getting lost...


I agree with Kleber S, you should move this into a helper, because that's a lot of logic for a view

def crews_description(crews)
  if crews.empty?
    content_tag('em', 'Independent')
    label = "Crew"
    label = label.pluralize if crews.size > 1
    crews_links = {|crew| link_to(h(, crew)}.to_sentence
    content_tag('span', label) + crews_links.html_safe

and in your view:

<span class="affiliation">
  <%= crews_description(@bboy.crews)

Rails 3 now automatically escapes everything, in order to output raw HTML use this:

<%= some_string.html_safe %>

or this:

<%= raw @some_html_string %>

Thanks to macek for a hint.

For additional details:

You can (and should) you the raw method

<%= raw @some_html_string %>

I recommend you move this block of code to an helper and then use the .html_safe method to obtain the expected results.

Need Your Help

Global `before` and `beforeEach` for mocha?

javascript unit-testing mocha

I'm using mocha for javascript unit-testing now.