Encodeuricomponent decoding it in rails

ajax, encoding, javascript, ruby-on-rails, url

Solution

Rails "automagically" handles parameters with the following logic.

If the request is GET it will decode anything in the query string:

GET http://server/controller?value=ab%23cd
  On the server this will generate params['value'] as ab#cd

If the request is a POST with a query string it will not decode it:

POST http://server/controller?value=ab%23cd
  On the server this will generate params['value'] as ab%23cd

If the request is a POST with data parameters, it will decode it:

POST http://server/controller
  data: value=ab%23cd
  On the server this will generate params['value'] as ab#cd

I suspect that you're seeing this issue because you're including a query string with a `POST` request instead of a `GET` request and so Rails isn't decoding the query string.

Problem

Normally rails magically decodes all `params`. Now I got a javascript which does `params="value="+encodeURIComponent('ab#cd');` and then calls `http://server/controller?value=ab%23cd`. If I access `params[:value]` in my controller, it contains `ab%23cd` and not `ab#cd` as I would expect. How to solve this? Why does rails no auto decoding of this param?

Original source