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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With