Rails 3 link_to inside string

Every time I try to combine a string with link_to, it displays in my browser as escaped HTML.

eg.

%(TEST #{link_to(object.title, object)})

OUTPUTS

TEST <a href="/objects/3">TEST OBJECT</a> 

Why is this happening? Every example that I see on the network is not referenced by link_to.

+3
source share
2 answers

In Rails 3, output is escaped by default. If you add .html_safeto your line, it will do what you expect.

%(TEST #{link_to(object.title, object)}).html_safe
+12
source

This also does the job:

<%=raw %(TEST #{link_to(object.title, object)}) %>
+4
source

All Articles