Today I Learned

hashrocket A Hashrocket project

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"}
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.