views:

74

answers:

1

Rails appears to be converting the ampersand at the beginning of the utf-8 entity to an HTML entity: &

So ▲ becomes ▲ but I would like to display a downward arrow instead, which is what the utf-8 entity would normally be.

I'm using Rails 2.3.8 and Ruby 1.8.7.

Here is what the view looks like:

<%= get_arrow_from_helper(order) %>

And here is what the helper looks like:

def get_arrow_from_helper(order)
    arrow = order == "ASC" ? "&#x25B2;" : "&#x25BC;"
    html = "<div>#{arrow}</div>"
    return html
end
+2  A: 

Which Rails version are you using? If you are using Rails 3 (or rails_xss plugin), Rails will escape the content by default to prevent XSS injection.

<%= "Copyright &copy; 2010" %>

will print out

Copyright &amp;copy; 2010

There are several ways:

  1. use the raw helper, but only if you are sure the content you are printing comes from a safe source.

    <%= raw "Copyright &copy; 2010" %>
    
  2. mark the string as safe, but only if you are sure the string is really safe

    <%= "Copyright &copy; 2010".html_safe %>
    
Simone Carletti
I'm using Rails 2.3.8.
fisherwebdev
This did work, but for whatever reason, my document is not interpreting the utf-8 correctly. Not sure why. But these methods did help to stop Rails from converting the ampersand.
fisherwebdev
Oops! I was using decimal entities instead of hexidecimal. I've updated the question so it's correct now. The solutions offered by Simone work great. Thanks Simone! More on entities: http://santagata.us/characters/CharacterEntities.html
fisherwebdev