I have a link_to
helper like the following:
<%= link_to "example & text", url_for(:controller =>'example', :title=>"example & text") %>
It frames the URL http://localhost:3000/example?title=example&:text
In the sample controller it calls the index
method but params[:title]
returns
the value example&:text
.
I want to have a value like "example & text". So I have tried
CGI::escape()
and CGI::escapeHTML()
but without luck.
The url needs to be escaped using CGI.escape
:
link_to "example & text", :controller => "example", :title => CGI.escape("example & text")
This should generate something like:
<a href="/example?title=example+%26+text">example & text</a>
Then, wherever you're wanting to use this, you can unescape it again to get it back to normal:
CGI.unescape params[:title] # => "example & text"
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