Strong Parameters with arrays
Need to permit a strong parameter that can either be an array or a string? Just permit it twice!
# This is just for shortened syntax for this TIL.
class Param < ActionController::Paramaeters
end
Param.new({foo: "bar"}).permit(:foo)
#=> { "foo" => "bar" }
Param.new({foo: "bar"}).permit(foo: [])
#=> { }
Param.new({foo: ["bar"]}).permit(foo: [])
#=> { "foo" => ["bar"] }
Param.new({foo: ["bar"]}).permit(:foo)
#=> { }
Param.new({foo: ["bar"]}).permit(:foo, foo: [])
#=> { "foo" => ["bar"] }
Param.new({foo: "bar"}).permit(:foo, foo: [])
#=> { "foo" => "bar"}
Tweet