Add a file lib/string_patch.rb
class String
def underscore
self.gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
.tr('-', '_')
.downcase
end
end
Be sure to eager load this file in application.rb
with
require('lib/string_patch.rb')
In your controller, you’ll need to use a syntax like this to convert camelCase to snake_case:
User.create(user_params.keys.collect { |k| [k.underscore.to_sym, user_params[k]] }.to_h)