# Format a string in params

**URL:** <https://www.ruby-forum.com/t/format-a-string-in-params/87004>\
**Category:** Rails\
**Created:** [February 27, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004 "2007-02-27T15:25:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/1 "2007-02-27T15:25:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/2 "2007-02-27T15:39:01Z")

</div>

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

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/3 "2007-02-27T16:46:14Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/4 "2007-02-27T17:08:18Z")

</div>

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, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/5 "2007-02-27T17:28:23Z")

</div>

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](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html)

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/6 "2007-02-27T17:21:53Z")

</div>

removed\_email\_address@domain.invalid 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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/7 "2007-02-27T17:29:24Z")

</div>

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

On 27 fév, 17:21, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 27, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/8 "2007-02-27T21:14:31Z")

</div>

> 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](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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 28, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/format-a-string-in-params/87004/9 "2007-02-28T20:36:54Z")

</div>

thank you
