In rails 4.2, the error Encoding:: UndefinedConversionError - "\xE6" from ASCII-8BIT to UTF-8:.

Asked 2 years ago, Updated 2 years ago, 82 views

ruby 2.2.7
rails 4.2.9

If you skip the parameters in form_for as shown below, the contents of the parameters will be ascii-8bit and Encoding::CompatibilityError will appear.

=form_for@hoge, :url=>{:action=>:update} do | f |
  = hidden_field_tag('authenticity_token', form_authenticity_token)
  = f.submit "Save"

The parameters are as follows:Look at the utf8 section.

Processing by HogeController #update as HTML
  Parameters: {"utf8"=>"\xE2\x9C\x93", "authenticity_token"=>"jWj10656mdI5VHn+sa}

rails 4.1.16, as intended.

Processing by HogeController #update as HTML
  Parameters: {"utf8"=>" "", "authenticity_token"=>"jWj10656mdI5VHn+sa}

I can't even consider what might be the cause.Thank you.
"If you have any additional information, I may be able to consider it, so please let me know as I will submit it additional information.

By the way, Gemfile is as follows


source 'http://rubygems.org'

gem 'rails', '4.2.9'
gem 'rake', '10.0.4'

gem 'mysql2', '0.3.14'

gem'switch_point', '0.8.0'
gem 'google-api-client', '0.8.0'
gem'tzinfo', '~>1.1'
gem "paranoia", "~>2.2"
gem 'jquery-rails', '~> 3.0'
gem 'will_paginate', '~>3.0'
gem'selectable_attr_rails', '0.3.14'
gem'selectable_attr', '0.3.17'
gem'acts_as_tree'
gem 'rmagic', '2.16.0'
gem 'date_validator'
gem 'crummy', '1.2'
gem 'pdfkit', '0.5.2'
gem'sanitizes', '2.0.6'
gem 'jpmobile', '3.0.2'#:git=>"git://github.com/jpmobile/jpmobile.git",:tag=>"v1.0.0.pre"
gem 'garb', '0.9.1'
gem 'settingslogic', '2.0.8'
gem'unicorn', '4.8.3'
gem 'omniauth-facebook', '4.0.0'

gem 'delayed_job', '~>4.0.3'
gem 'delayed_job_active_record', '~>4.0.2'
gem 'daemons', '1.1.8'
gem'rack_after_reply'

gem "romankana", "~>0.1.3"
gem'kaminari'
gem 'active_decorator'
gem "RubyInline"

source'http://gems.github.com/'
gem 'google charts', '1.6.8'

gem "redis", "2.2.1"
gem "redis-rails"
gem "redis-objects"
gem 'activerecord-session_store'

gem 'rack-pjax'
gem 'fluent-logger'
gem 'fingerprintjs-rails'

gem 'natto'
gem 'active_hash'
gem'public_suffix'

gem "msgpack"

gem 'react-rails', '~>1.0'
gem'slim-rails'
gem 'business_time'

gem'test-unit', '~>3.0'

gem 'rails-observers'

gem 'actionpack-page_caching'

gem 'activesource', require: 'active_resource'

gem'sass-rails', '5.0.6'
gem 'compass-rails', '3.0.2'
gem 'uglifer', '>=1.0.3'
gem 'execjs'

Add the following to config/application.rb

config.middleware.insert_before ActionDispatch::ParamsParser, "ForceParamsEncoding"

Create a file called lib/test.rb

class ForceParamsEncoding
  def initialize (app)
    @app=app
  end

  def call(env)
    @request=Rack::Request.new(env)
    [email protected]
    params.each do|k,v|
      if v.class == Array
        array=[ ]
        array<<force_encoding_utf_1(v[0])
        params[k] = array
      elsif v.class==Hash
        params[k] = force_encoding_utf_1(v)
      else
        params[k] = v.force_encoding("UTF-8")
      end
    end
    @ app.call(env)
  end

  def force_encoding_utf_1(value)
    value.each do|k,v|
      if v.class == Array
        array=[ ]
        array<<force_encoding_utf_2(v[0])
        value[k] = array
      elsif v.class==Hash
        value[k] = force_encoding_utf_2(v)
      else
        value[k] = v.force_encoding("UTF-8")
      end
    end
    return value
  end

  def force_encoding_utf_2(value)
    value.each do|k,v|
      if v.class == Array
        array=[ ]
        array<<force_encoding_utf_1(v[0])
        value[k] = array
      elsif v.class==Hash
        value[k] = force_encoding_utf_1(v)
      else
        value[k] = v.force_encoding("UTF-8")
      end
    end
    return value
  end
end

(It's a memo, so it's a very dirty code.)

I have responded once to force require"#{Rails.root}/lib/test" to fix it.

If you know how to do it separately, it would be helpful if you could throw it at any time.

I received a comment saying that the gem is bad, so I would like to add it as an additional 3.
I forced it to be fixed once, so I will add it.I think it's better than postscript 1.


lib/rack/utils.rb


     def parse_nested_query(qs,d=nil)
       params=KeySpaceConstrainedParams.new

       (qs||').split(d?/[#{d}]*/n: DEFAULT_SEP).each do|p|
         k,v=p.split('=',2).map {|s|unescape(s)}
     # The unescape(s) part is URI.decode_www_form_component(s, Encoding::UTF_8).There is a high possibility that something is wrong with the unescape method.

         normalize_params(params,k,v)
       end
       return params.to_params_hash
     rescue ArgumentError=>e
       raise InvalidParameterError, e.message
     end

ruby-on-rails encoding

2022-09-29 21:26

1 Answers

The cause has been identified.As you pointed out, the gem was bad.


gem 'jpmobile', '3.0.2'

I updated this and it's fixed.


gem 'jpmobile', '~>4.2'


2022-09-29 21:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.