johnnycakes
johnnycakes

Reputation: 2450

Ruby/Rails - Is there an easy way to make hard-coded HTML symbols html_safe?

In my view I want to display some right double angle quotes in my link.

Before Rails 3, this worked:

<%= link_to "&raquo; #{@category.name}", some_path %>

Now what should I do if I want to specify the &raquo; as html_safe but not the rest of the link's text?

In other words I do not want to do this:

<%= link_to "&raquo; #{@category.name}".html_safe, some_path %> I do not want the @category.name treated as html_safe.

This produces the desired result:

<%= link_to "&raquo;".html_safe + " #{@category.name}", some_path %>

However, if I do this:

<%= link_to "#{@category.name}" + "&raquo;".html_safe, some_path %>

The output of the angle quotes is not treated as safe. I see &raquo; on my page and not ».

Why?

I tried extracting "&raquo;".html_safe to a helper method with the same results.

Is there a way to easily designate hard coded text/symbols as HMTL safe in Rails 3?

Thanks.

Upvotes: 3

Views: 1231

Answers (2)

Tilo
Tilo

Reputation: 33752

you need to make sure that the whole string is html_safe...

I'd recommend to try this:

   "&raquo; #{h @cagegory.name}".html_safe

Upvotes: 0

Micha&#235;l Witrant
Micha&#235;l Witrant

Reputation: 7714

In this situation I often explicitly escape the unsafe part:

"&raquo; #{h @category.name}".html_safe

Upvotes: 3

Related Questions