Encodeuricomponent decodes it in rails

Usually rails magically decode all params . Now I have javascript that 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 , not ab#cd , as I would expect.

How to solve this? Why don't the rails automatically decode this parameter?

+4
source share
1 answer

The rails "automatically" process the parameters with the following logic.

If the request is GET, it will decode something 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 you are seeing this problem because you include the query string with the POST request instead of the GET request, and therefore Rails does not decrypt the query string.

+3
source

Source: https://habr.com/ru/post/1441653/


All Articles