Skip to content

Instantly share code, notes, and snippets.

@MGPalmer
Created March 13, 2012 09:53
Show Gist options
  • Save MGPalmer/2027920 to your computer and use it in GitHub Desktop.
Save MGPalmer/2027920 to your computer and use it in GitHub Desktop.
Enforce whitelist mode for mass assignment for Rails < 3.2
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
# (this is a config option in Rails 3.2)
ActiveRecord::Base.send(:attr_accessible, nil)
if %W(development test).include?(Rails.env)
# Raise exception on mass assignment protection for ActiveRecord models
# (this is a config option in Rails 3.2)
module ActiveModel
module MassAssignmentSecurity
module Sanitizer
def warn!(attrs)
raise "Can't mass-assign protected attributes: #{attrs.join(', ')}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment