Format a string in params

Hi,
i’ve got a problem with slashes “/” :
i do a redirect :

redirect_to :action => action, :id => params[:my_param]

but when parameter string params[:my_param] is with slash, for example
“16/9”, i’ve got the following error

no route found to match “/controller/action/16/9” with {:method=>:get}

does anybody know a function to format that kind of parameter to get a
valid url?

Hi, what does 16/9 represent? In any case, you’ll need to sanitize,
params[:my_param], before you redirect.

Good luck,

-Conrad

Thank you for your answer.

I’ve found a solution : encode parameter.
put in the controller header :
require ‘cgi’
before send encode parameter :
CGI::escape(params[:my_param])
at reception decode it :
CGI::unescape(params[:my_param])

Then, i keep valid url :slight_smile:

You don’t need to use the CGI escape, Rails gives you the same thing,
it is called h (alias for html_escape) that does the same thing.

On Feb 27, 10:43 am, “[email protected]

No, html_escape as the same means translate html.
To translate url we have to use url_encode :
http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html

but i didn’t find the way to decode it…

[email protected] wrote:

You don’t need to use the CGI escape, Rails gives you the same thing,
it is called h (alias for html_escape) that does the same thing.

URL encoding is NOT the same thing as HTML escaping. I don’t see h() in
the api docs, but I’m pretty sure it would leave “/” alone since it
doesn’t need to be escaped in HTML.

Sorry mark i’ve posted my message at the same time you did

On 27 fév, 17:21, Mark T. [email protected]

No, html_escape as the same means translate html.
To translate url we have to use url_encode :http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html

Ah, the u() method, a counterpart to h().

but i didn’t find the way to decode it…

Rails decodes URLs automatically. However, if for some reason you
needed to do this yourself:

def url_decode(str)
str.to_s.gsub(/%([A-F\d]{2})/in) { [$1.hex].pack(‘C’) }
end

  • Mark.

thank you