Rails: Using require and permit for attributes

Posted . Visible to the public.

Raising errors for required and permitted attributes makes it easier to find errors in your application during development and in tests. Consider this approach if you want to strengthen the params handling in your application.

Example

config.action_controller.action_on_unpermitted_parameters = :raise
def user_params
  params.require(:user).permit(:full_name)
end

Effects

  • This raises an error ActionController::ParameterMissing if there is no required parameter:

  • This raises an error ActionController::UnpermittedParameters if there is an unpermitted parameter:

    • You need to manually merge it to the rescue_responses in case users should see a bad request error page.
    • You need to add it manually to sentry on the ignore list in case it should not be reported as exception.
Last edit
Emanuel
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2024-05-22 14:14)