Joshua Muheim
Joshua Muheim

Reputation: 13243

Strong Parameters: display error when sent param is not in allowed list (in development)

Is there a way to display an error when a sent param is not in allowed list (in development)?

I very often forget to add the param to the list and don't notice it in the first place.

Upvotes: 0

Views: 752

Answers (1)

pdobb
pdobb

Reputation: 18037

I don't think StrongParameters does this already, but you're free to do it on your own. For example:

def object_params
  permitted_params = [:a, :b, :c]
  params.require(:some_object_name).permit(*permitted_params)
  if Rails.env.development? && permitted_params.exclude?(some_param)
    # TODO: Raise an exception or log an error or whatever you want to do here.
  end
end

UPDATE

I found out the strong_parameters gem (included by default in Rails 4) does allow for some flexibility here! See this section of the readme: https://github.com/rails/strong_parameters#handling-of-unpermitted-keys. So, in development environment the unpermitted keys should be logged already. And you can change that to do a raise instead if you want. Cool! If this isn't exactly what you want and the above code isn't either then you can probably monkey-patch or fork the gem to do something special with unpermitted keys across your entire app.

Upvotes: 1

Related Questions