Skip to content

Instantly share code, notes, and snippets.

@tjmcewan
Created July 13, 2011 14:29
Show Gist options
  • Save tjmcewan/1080395 to your computer and use it in GitHub Desktop.
Save tjmcewan/1080395 to your computer and use it in GitHub Desktop.
raise exception on attempted mass assignment of a protected attribute
# monkey-patch to raise an exception when attempting to mass-assign a protected attr
# more info: http://is.gd/MiA1mn
module ActiveModel
module MassAssignmentSecurity
module Sanitizer
protected
def warn!(attrs)
self.logger.debug "WARNING: Can't mass-assign protected attributes: #{attrs.join(', ')}" if self.logger
raise RuntimeError, "these attributes are protected from mass assignment, use `attr_accessible :#{attrs.join(', :')}`"
end
end
end
end
@tjmcewan
Copy link
Author

it'd be great to also add in the name of failing class. wasn't immediately obvious to me so I left it. suggestions welcomed. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment