Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I output raw unescaped html from this Rails route/helpers?

I am trying to use the following bit of code to generate a link on my page

%h2= link_to '{{ label }}', product_path('{{ id }}'.html_safe)

This is actually part of a larger HTML block which will serve as a javascript template, and I will later parse using Underscore.js to fill in the {{ id }} and {{ label }} placeholders. So I would like rails to output something to my HTML like

/products/{{ id }}

However, it keeps escaping the spaces and brackeets, and giving me

<a href="/products/%7B%7B%20id%20%7D%7D">{{ label }}</a>

So the url_helper is escaping my string, even though I don't want it to. How can I force it to not do this?

I've tried

%h2= link_to '{{ label }}', product_path('{{ id }}'.html_safe)
%h2= link_to '{{ label }}', product_path(raw '{{ id }}')
%h2= link_to '{{ label }}', raw(product_path('{{ id }}'))

and

%h2=raw( link_to '{{ label }}', product_path('{{ id }}'.html_safe))

But none of them work

EDIT:

Another way to play with this is from rails console,

include ActionController::UrlWriter

ruby-1.9.2-p0 :010 > product_path '{{ id }}'.html_safe
 => "/products/%7B%7B%20id%20%7D%7D" 

Any help appreciated... thanks

Thanks

like image 888
noli Avatar asked Jul 04 '11 02:07

noli


1 Answers

What about CGI::unescape(product_path('{{ id }}') ? (with the require 'cgi' that goes with it.)

I believe this is Ruby 1.9.2 only but it seems to be the version you're using.

like image 197
dee-see Avatar answered Oct 12 '22 04:10

dee-see