Unescape special characters correctly from the URL in Rails 3.0.3 Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Strange InvalidURIError for '|' character?How do I call controller/view methods from the console in Rails?How do I get the current absolute URL in Ruby on Rails?How to extract URL parameters from a URL with Ruby or Rails?How can I download a file from a URL and save it in Rails?How to remove a key from Hash and get the remaining hash in Ruby/Rails?Rails: select unique values from a columnprevent rails from escaping special characters in url paramHow to sort with elasticsearch?Jsoup unescapes special charactersRails Mysql2::Error: Use of count to get group

Is there a verb for listening stealthily?

What is the numbering system used for the DSN dishes?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

How would you suggest I follow up with coworkers about our deadline that's today?

Like totally amazing interchangeable sister outfit accessory swapping or whatever

What's parked in Mil Moscow helicopter plant?

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

Will I be more secure with my own router behind my ISP's router?

Is there a way to fake a method response using Mock or Stubs?

What does the black goddess statue do and what is it?

How to begin with a paragraph in latex

Why aren't road bicycle wheels tiny?

Processing ADC conversion result: DMA vs Processor Registers

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

France's Public Holidays' Puzzle

Arriving in Atlanta (after US Preclearance in Dublin). Will I go through TSA security in Atlanta to transfer to a connecting flight?

How to keep bees out of canned beverages?

What to do with someone that cheated their way though university and a PhD program?

Is a self contained air-bullet cartridge feasible?

Why I cannot instantiate a class whose constructor is private in a friend class?

Why isn't everyone flabbergasted about Bran's "gift"?

What do you call an IPA symbol that lacks a name (e.g. ɲ)?

What is the ongoing value of the Kanban board to the developers as opposed to management

Why would the Overseers waste their stock of slaves on the Game?



Unescape special characters correctly from the URL in Rails 3.0.3



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Strange InvalidURIError for '|' character?How do I call controller/view methods from the console in Rails?How do I get the current absolute URL in Ruby on Rails?How to extract URL parameters from a URL with Ruby or Rails?How can I download a file from a URL and save it in Rails?How to remove a key from Hash and get the remaining hash in Ruby/Rails?Rails: select unique values from a columnprevent rails from escaping special characters in url paramHow to sort with elasticsearch?Jsoup unescapes special charactersRails Mysql2::Error: Use of count to get group



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6'



There's a search feature in my project that enable people to use the GET method using URL or using forms and then generate the URL.



Example:



I want to search:



origin city: "Århus, Denmark" and destination city: "Asunción, Paraguay"



they both have a special character: "Å" and "ó", so the URL will be generated like this when someone click the search button.



?&origin=%C5rhus%2C%20Denmark&destination=Asunci%F3n%2C%20Paraguay


Problem:



When i search that city, it's not unescaped like i want ( i tried using like CGI, URI, even some gems).



When i see at the console, ActiveRecord received the query like this:



Parameters: "destination"=>"Asunci�n, Paraguay", "origin"=>"�rhus, Denmark", "sort"=>"newest"
City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = '�rhus') ORDER BY cities.name ASC
City Load (6.8ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = 'Asunci�n, Paraguay') ORDER BY cities.name ASC


Conclusion: the cities can't be found :(



But, i found an interesting thing:




  • When i made an error on the file asociated with this function, the output will be like this :



    Request



    Parameters:
    "destination"=>"Asunción,
    Paraguay",
    "origin"=>"Århus,
    Denmark",
    "sort"=>"newest"


it's a valid one!



Question:



Do you guys have an idea how to solve this? Thanks in advance :)










share|improve this question






























    7















    I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6'



    There's a search feature in my project that enable people to use the GET method using URL or using forms and then generate the URL.



    Example:



    I want to search:



    origin city: "Århus, Denmark" and destination city: "Asunción, Paraguay"



    they both have a special character: "Å" and "ó", so the URL will be generated like this when someone click the search button.



    ?&origin=%C5rhus%2C%20Denmark&destination=Asunci%F3n%2C%20Paraguay


    Problem:



    When i search that city, it's not unescaped like i want ( i tried using like CGI, URI, even some gems).



    When i see at the console, ActiveRecord received the query like this:



    Parameters: "destination"=>"Asunci�n, Paraguay", "origin"=>"�rhus, Denmark", "sort"=>"newest"
    City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = '�rhus') ORDER BY cities.name ASC
    City Load (6.8ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = 'Asunci�n, Paraguay') ORDER BY cities.name ASC


    Conclusion: the cities can't be found :(



    But, i found an interesting thing:




    • When i made an error on the file asociated with this function, the output will be like this :



      Request



      Parameters:
      "destination"=>"Asunción,
      Paraguay",
      "origin"=>"Århus,
      Denmark",
      "sort"=>"newest"


    it's a valid one!



    Question:



    Do you guys have an idea how to solve this? Thanks in advance :)










    share|improve this question


























      7












      7








      7


      1






      I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6'



      There's a search feature in my project that enable people to use the GET method using URL or using forms and then generate the URL.



      Example:



      I want to search:



      origin city: "Århus, Denmark" and destination city: "Asunción, Paraguay"



      they both have a special character: "Å" and "ó", so the URL will be generated like this when someone click the search button.



      ?&origin=%C5rhus%2C%20Denmark&destination=Asunci%F3n%2C%20Paraguay


      Problem:



      When i search that city, it's not unescaped like i want ( i tried using like CGI, URI, even some gems).



      When i see at the console, ActiveRecord received the query like this:



      Parameters: "destination"=>"Asunci�n, Paraguay", "origin"=>"�rhus, Denmark", "sort"=>"newest"
      City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = '�rhus') ORDER BY cities.name ASC
      City Load (6.8ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = 'Asunci�n, Paraguay') ORDER BY cities.name ASC


      Conclusion: the cities can't be found :(



      But, i found an interesting thing:




      • When i made an error on the file asociated with this function, the output will be like this :



        Request



        Parameters:
        "destination"=>"Asunción,
        Paraguay",
        "origin"=>"Århus,
        Denmark",
        "sort"=>"newest"


      it's a valid one!



      Question:



      Do you guys have an idea how to solve this? Thanks in advance :)










      share|improve this question
















      I'm using Rails 3.0.3 with REE ( Ruby 1.8.7 ) and gem 'mysql2', '0.2.6'



      There's a search feature in my project that enable people to use the GET method using URL or using forms and then generate the URL.



      Example:



      I want to search:



      origin city: "Århus, Denmark" and destination city: "Asunción, Paraguay"



      they both have a special character: "Å" and "ó", so the URL will be generated like this when someone click the search button.



      ?&origin=%C5rhus%2C%20Denmark&destination=Asunci%F3n%2C%20Paraguay


      Problem:



      When i search that city, it's not unescaped like i want ( i tried using like CGI, URI, even some gems).



      When i see at the console, ActiveRecord received the query like this:



      Parameters: "destination"=>"Asunci�n, Paraguay", "origin"=>"�rhus, Denmark", "sort"=>"newest"
      City Load (0.1ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = '�rhus') ORDER BY cities.name ASC
      City Load (6.8ms) SELECT `cities`.* FROM `cities` WHERE (`cities`.`name` = 'Asunci�n, Paraguay') ORDER BY cities.name ASC


      Conclusion: the cities can't be found :(



      But, i found an interesting thing:




      • When i made an error on the file asociated with this function, the output will be like this :



        Request



        Parameters:
        "destination"=>"Asunción,
        Paraguay",
        "origin"=>"Århus,
        Denmark",
        "sort"=>"newest"


      it's a valid one!



      Question:



      Do you guys have an idea how to solve this? Thanks in advance :)







      ruby-on-rails ruby activerecord encoding escaping






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 17 '12 at 7:35







      panggi

















      asked Jan 17 '12 at 3:11









      panggipanggi

      3815




      3815






















          1 Answer
          1






          active

          oldest

          votes


















          12














          You're right, it looks like you have an encoding problem somewhere. The 0xC5 character is "Å" in ISO-8859-1 (AKA Latin-1), in UTF-8 it would be %C3%85 in the URL.



          I suspect that you're using JavaScript on the client side and that your JavaScript is using the old escape function to build the URL, escape has some issues with non-ASCII characters. If this is the case, then you should upgrade your JavaScript to use encodeURIComponent instead. Have a look at this little demo and you'll see what I'm talking about:




          http://jsfiddle.net/ambiguous/U5A3k/




          If you can't change the client-side script then you can do it the hard way in Ruby using force_encoding and encoding:



          >> s = CGI.unescape('%C5rhus%2C%20Denmark')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>
          >> s.force_encoding('iso-8859-1')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:ISO-8859-1>
          >> s.encode!('utf-8')
          => "Århus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>


          You should get something like "xC5rhus, Denmark" from params and you could unmangle that with:



          s = params[:whatever].force_encoding('iso-8859-1').encode('utf-8')


          Dealing with this on the server side would be a last resort though, if your client-side code is sending back incorrectly encoded data then you'll be left with a pile of guesswork on the server to figure out what encoding was actually used to get it into the URL.






          share|improve this answer

























          • Yay! i used 'escape' function to build the URL in Javascript before, and now i'm using 'encodeURIComponent' as you told and it works :D Thank you for saving my day :)

            – panggi
            Jan 17 '12 at 4:39











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8889220%2funescape-special-characters-correctly-from-the-url-in-rails-3-0-3%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          12














          You're right, it looks like you have an encoding problem somewhere. The 0xC5 character is "Å" in ISO-8859-1 (AKA Latin-1), in UTF-8 it would be %C3%85 in the URL.



          I suspect that you're using JavaScript on the client side and that your JavaScript is using the old escape function to build the URL, escape has some issues with non-ASCII characters. If this is the case, then you should upgrade your JavaScript to use encodeURIComponent instead. Have a look at this little demo and you'll see what I'm talking about:




          http://jsfiddle.net/ambiguous/U5A3k/




          If you can't change the client-side script then you can do it the hard way in Ruby using force_encoding and encoding:



          >> s = CGI.unescape('%C5rhus%2C%20Denmark')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>
          >> s.force_encoding('iso-8859-1')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:ISO-8859-1>
          >> s.encode!('utf-8')
          => "Århus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>


          You should get something like "xC5rhus, Denmark" from params and you could unmangle that with:



          s = params[:whatever].force_encoding('iso-8859-1').encode('utf-8')


          Dealing with this on the server side would be a last resort though, if your client-side code is sending back incorrectly encoded data then you'll be left with a pile of guesswork on the server to figure out what encoding was actually used to get it into the URL.






          share|improve this answer

























          • Yay! i used 'escape' function to build the URL in Javascript before, and now i'm using 'encodeURIComponent' as you told and it works :D Thank you for saving my day :)

            – panggi
            Jan 17 '12 at 4:39















          12














          You're right, it looks like you have an encoding problem somewhere. The 0xC5 character is "Å" in ISO-8859-1 (AKA Latin-1), in UTF-8 it would be %C3%85 in the URL.



          I suspect that you're using JavaScript on the client side and that your JavaScript is using the old escape function to build the URL, escape has some issues with non-ASCII characters. If this is the case, then you should upgrade your JavaScript to use encodeURIComponent instead. Have a look at this little demo and you'll see what I'm talking about:




          http://jsfiddle.net/ambiguous/U5A3k/




          If you can't change the client-side script then you can do it the hard way in Ruby using force_encoding and encoding:



          >> s = CGI.unescape('%C5rhus%2C%20Denmark')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>
          >> s.force_encoding('iso-8859-1')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:ISO-8859-1>
          >> s.encode!('utf-8')
          => "Århus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>


          You should get something like "xC5rhus, Denmark" from params and you could unmangle that with:



          s = params[:whatever].force_encoding('iso-8859-1').encode('utf-8')


          Dealing with this on the server side would be a last resort though, if your client-side code is sending back incorrectly encoded data then you'll be left with a pile of guesswork on the server to figure out what encoding was actually used to get it into the URL.






          share|improve this answer

























          • Yay! i used 'escape' function to build the URL in Javascript before, and now i'm using 'encodeURIComponent' as you told and it works :D Thank you for saving my day :)

            – panggi
            Jan 17 '12 at 4:39













          12












          12








          12







          You're right, it looks like you have an encoding problem somewhere. The 0xC5 character is "Å" in ISO-8859-1 (AKA Latin-1), in UTF-8 it would be %C3%85 in the URL.



          I suspect that you're using JavaScript on the client side and that your JavaScript is using the old escape function to build the URL, escape has some issues with non-ASCII characters. If this is the case, then you should upgrade your JavaScript to use encodeURIComponent instead. Have a look at this little demo and you'll see what I'm talking about:




          http://jsfiddle.net/ambiguous/U5A3k/




          If you can't change the client-side script then you can do it the hard way in Ruby using force_encoding and encoding:



          >> s = CGI.unescape('%C5rhus%2C%20Denmark')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>
          >> s.force_encoding('iso-8859-1')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:ISO-8859-1>
          >> s.encode!('utf-8')
          => "Århus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>


          You should get something like "xC5rhus, Denmark" from params and you could unmangle that with:



          s = params[:whatever].force_encoding('iso-8859-1').encode('utf-8')


          Dealing with this on the server side would be a last resort though, if your client-side code is sending back incorrectly encoded data then you'll be left with a pile of guesswork on the server to figure out what encoding was actually used to get it into the URL.






          share|improve this answer















          You're right, it looks like you have an encoding problem somewhere. The 0xC5 character is "Å" in ISO-8859-1 (AKA Latin-1), in UTF-8 it would be %C3%85 in the URL.



          I suspect that you're using JavaScript on the client side and that your JavaScript is using the old escape function to build the URL, escape has some issues with non-ASCII characters. If this is the case, then you should upgrade your JavaScript to use encodeURIComponent instead. Have a look at this little demo and you'll see what I'm talking about:




          http://jsfiddle.net/ambiguous/U5A3k/




          If you can't change the client-side script then you can do it the hard way in Ruby using force_encoding and encoding:



          >> s = CGI.unescape('%C5rhus%2C%20Denmark')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>
          >> s.force_encoding('iso-8859-1')
          => "xC5rhus, Denmark"
          >> s.encoding
          => #<Encoding:ISO-8859-1>
          >> s.encode!('utf-8')
          => "Århus, Denmark"
          >> s.encoding
          => #<Encoding:UTF-8>


          You should get something like "xC5rhus, Denmark" from params and you could unmangle that with:



          s = params[:whatever].force_encoding('iso-8859-1').encode('utf-8')


          Dealing with this on the server side would be a last resort though, if your client-side code is sending back incorrectly encoded data then you'll be left with a pile of guesswork on the server to figure out what encoding was actually used to get it into the URL.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 9 at 4:44

























          answered Jan 17 '12 at 3:48









          mu is too shortmu is too short

          355k58702677




          355k58702677












          • Yay! i used 'escape' function to build the URL in Javascript before, and now i'm using 'encodeURIComponent' as you told and it works :D Thank you for saving my day :)

            – panggi
            Jan 17 '12 at 4:39

















          • Yay! i used 'escape' function to build the URL in Javascript before, and now i'm using 'encodeURIComponent' as you told and it works :D Thank you for saving my day :)

            – panggi
            Jan 17 '12 at 4:39
















          Yay! i used 'escape' function to build the URL in Javascript before, and now i'm using 'encodeURIComponent' as you told and it works :D Thank you for saving my day :)

          – panggi
          Jan 17 '12 at 4:39





          Yay! i used 'escape' function to build the URL in Javascript before, and now i'm using 'encodeURIComponent' as you told and it works :D Thank you for saving my day :)

          – panggi
          Jan 17 '12 at 4:39



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8889220%2funescape-special-characters-correctly-from-the-url-in-rails-3-0-3%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко