Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to escape ampersand in URL formation

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&amp:text

In the sample controller it calls the index method but params[:title] returns the value example&amp:text.

I want to have a value like "example & text". So I have tried CGI::escape() and CGI::escapeHTML() but without luck.

like image 565
palani Avatar asked Jan 25 '11 10:01

palani


1 Answers

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"
like image 149
idlefingers Avatar answered Oct 04 '22 04:10

idlefingers