Beware using model named Format in a Rails API

Vijay Goel, MD
Building the Stack
Published in
1 min readDec 5, 2018

Just spent a fair amount of time troubleshooting an error that was throwing a really weird strong parameters bug. Something along the lines of a “JSON”:String not having a method permit. It was in my FormatsController that was a copy paste from a previous WorkingController that had no problems with HTTP communication with the front end…so I was quite confused and spent a long time trying to find a magic typo that didn’t exist (or at least wasn’t causing this particular problem).

TL;DR. Don’t use a model named Format

Of course if you break it down

#FormatsController
...
private
def formats_params
params.require(:format).permit(:foo, :bar, :baz)
end

The permit function doesn’t work, because the :format param doesn’t exist — you’ll see this in your logs when you POST or PATCH. Instead of

### Ideal request paramsParameters: { "foo": "", "bar": "", "baz": "", "format":{"foo": "", "bar": "", "baz": "" }, ... }

You get

### Actual request params
Parameters: { "foo": "", "bar": "", "baz": "", ...}

Found the culprit when I looked further into dev tools. The actual params include a "format": "json" which then causes the problem with resolving params[:format]

Solution is pretty straightforward. Name your format model something else (or you can do something funky for dev purposes like stripping out the require(:format), but I don’t recommend this for anything other than confirming the source of the bug.

--

--

Vijay Goel, MD
Building the Stack

Improving operations via technology and structured thinking (current focus chefs and catering)