解决 Rails 中的 URI 字符编码问题
我正在使用 Ruby on Rails 应用程序,该应用程序对第三方服务进行客户端 API 调用。然后,它从第 3 方 API 获取字符串并用它们生成 URI。
不幸的是,由于编码错误,Rails 无法解析某些 URI。我尝试通过encodeURIComponent 运行字符串,但这并不能解决问题。
这是我收到的错误:
Processing ApplicationController#index (for 67.101.113.66 at 2010-08-12 23:24:55) [GET]
Parameters: {"name"=>"Camilo Andr\xE9s \xD1ustes", "recipient_id"=>"279"}
ArgumentError (invalid byte sequence in US-ASCII):
<internal:prelude>:8:in `synchronize'
/home/heroku_rack/lib/static_assets.rb:9:in `call'
/home/heroku_rack/lib/last_access.rb:25:in `call'
/home/heroku_rack/lib/date_header.rb:14:in `call'
thin (1.2.6) lib/thin/connection.rb:76:in `block in pre_process'
thin (1.2.6) lib/thin/connection.rb:74:in `catch'
thin (1.2.6) lib/thin/connection.rb:74:in `pre_process'
thin (1.2.6) lib/thin/connection.rb:57:in `process'
thin (1.2.6) lib/thin/connection.rb:42:in `receive_data'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
thin (1.2.6) lib/thin/backends/base.rb:57:in `start'
thin (1.2.6) lib/thin/server.rb:156:in `start'
thin (1.2.6) lib/thin/controllers/controller.rb:80:in `start'
thin (1.2.6) lib/thin/runner.rb:177:in `run_command'
thin (1.2.6) lib/thin/runner.rb:143:in `run!'
thin (1.2.6) bin/thin:6:in `<top (required)>'
/usr/ruby1.9.1/bin/thin:19:in `load'
/usr/ruby1.9.1/bin/thin:19:in `<main>'
如何正确处理这些字符串作为我的应用程序的输入?
I'm working with a Ruby on Rails application that makes a client side API call to a 3rd party service. It then takes strings from the 3rd party API and generates URIs with them.
Unfortunately, Rails fails to parse some of the URIs due to encoding errors. I have tried running the strings through encodeURIComponent, but this does not resolve the issue.
Here is the error I get:
Processing ApplicationController#index (for 67.101.113.66 at 2010-08-12 23:24:55) [GET]
Parameters: {"name"=>"Camilo Andr\xE9s \xD1ustes", "recipient_id"=>"279"}
ArgumentError (invalid byte sequence in US-ASCII):
<internal:prelude>:8:in `synchronize'
/home/heroku_rack/lib/static_assets.rb:9:in `call'
/home/heroku_rack/lib/last_access.rb:25:in `call'
/home/heroku_rack/lib/date_header.rb:14:in `call'
thin (1.2.6) lib/thin/connection.rb:76:in `block in pre_process'
thin (1.2.6) lib/thin/connection.rb:74:in `catch'
thin (1.2.6) lib/thin/connection.rb:74:in `pre_process'
thin (1.2.6) lib/thin/connection.rb:57:in `process'
thin (1.2.6) lib/thin/connection.rb:42:in `receive_data'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
thin (1.2.6) lib/thin/backends/base.rb:57:in `start'
thin (1.2.6) lib/thin/server.rb:156:in `start'
thin (1.2.6) lib/thin/controllers/controller.rb:80:in `start'
thin (1.2.6) lib/thin/runner.rb:177:in `run_command'
thin (1.2.6) lib/thin/runner.rb:143:in `run!'
thin (1.2.6) bin/thin:6:in `<top (required)>'
/usr/ruby1.9.1/bin/thin:19:in `load'
/usr/ruby1.9.1/bin/thin:19:in `<main>'
How do I properly handle these strings as input for my application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用:
<%= u name -%>
每当我必须在我的视图中传递身份验证令牌时,我都会使用这种技术:
<%= u form_authenticity_token %>
由于以上内容适用于视图,如果您想要特定于 ruby:
希望这会有所帮助。
This works for me:
<%= u name -%>
I use this technique everytime i have to pass on auth token around in my views:
<%= u form_authenticity_token %>
Since the above is for views, if you want ruby specific:
Hope this helps.