First time poster, long time Rails user, hoping to be contributing soon. I’m not sure if this has been covered before, I did search though, promise!
In any case, I fixed a bug at work recently, where we were sending a blank '' param value somehow (not super important in the context of this topic), which was then getting through params.fetch without raising an error.
Digging into the rails code, I found this curious as the error message specifically says:
ActionController::ParameterMissing: param is missing or the value is empty: :param_name
Interestingly, this doesn’t raise in our application for empty string, however the behaviour of empty? is to return true for an empty string ‘’.
''.empty?
=> true
Could/should this error message be improved? Or have I got it totally wrong? Look forward to hearing from someone who knows more than me about this!
It looks like only require checks for both key presence and value being .present?
While .fetch only cares about the key presence
Docs for .fetch don’t mention the presence check
So assuming both methods behave as designed we may have an opportunity to improve the error message and only have the or the value is empty part if raised from .require
Or maybe the message can be even smarter as require should be able to tell whether the value is empty or key is missing
Assuming that the docs for both methods are correct and these methods behave as expected I do believe that the error message will benefit from being a bit more specific