Rails: Configure ActionController to raise when strong params are invalid

Posted . Visible to the public.

Put the line below in the respective env.rb file to make your action controllers raise an ActionController::UnpermittedParameters error when strong params are not valid. This might come in handy when you are implementing an API and you want to let the user know that the request structure was invalid. You can then rescue those errors by implementing a rescue_from to have a generic handling of those cases. Note that you might need to whitelist common params such as :format to not raise on valid requests.

config.action_controller.action_on_unpermitted_parameters = :raise

Note: In case a required parameter params.require(:property).permit(...) was not provided or is nil, you will get a ActionController::ParameterMissing error. So you might want to rescue from both.

Last edit
Andreas Robecke
License
Source code in this card is licensed under the MIT License.
Posted to makandra dev (2017-07-16 13:06)