in Ruby

Specifying parameters encoding by action in Rails

At work I needed to accept a Windows-1250 encoding in URL parameters on some actions in one of our Rails API endpoint. The problem is that Rails will scream at you since everything needs to come as UTF-8. Future! Unfortunately some 3rd party Windows software might be still in the past… at that time I was trying to find a way how to write a middleware or patch Rails’ ActionPack to handle this request and what I found was actually a commit that adds this to Rails in master.

I was just skimming and browsing the code around
action_dispatch/http/parameters.rb being on master branch and found out about the upcoming support for custom encoding of Rails actions. It’s coming in this PR.

This does not handle nested parameters but I was still very happy to find about it.

As @tenderlove puts it “We know that this doesn’t handle nested parameters, but we don’t really need that right now.”

(Citation taken from the mentioned PR.)

And so, how one could use this upcoming feature (in Rails 5.1 I suppose)?

class ActionParameterEncodingController < ApplicationController
  parameter_encoding :param_in_win_encoding, :action_name, Encoding::WINDOWS_1250
  parameter_encoding :param_in_iso_encoding, :action_name, Encoding::ISO_8859_1

Important: As noted in comments, this will not get into Rails after all (I am sorry for misleading you), see

Write a Comment