Last Updated: February 25, 2016
· polimorfico

Force your params encoding in Rails

If you have problems with your params encodings, use a Rack middleware to encode them before rails params parsing code is executed.

First, build a class according to the signature of a rack middleware layer.

class ForceParamsEncoding
  def initialize(app)
    @app = app

  def call(env)
    @request =
    params = @request.params
    params.each { |k, v|  params[k] = v.force_encoding("ISO-8859-1").encode("UTF-8") }

Then register the middleware from application.rb.

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

And that's all! ^_^

1 Response
Add your response

Did you benchmark performance of this solution?

over 1 year ago ·