Solved: ActionController::ParameterMissing (param is missing or the value is empty)
When using Ruby on Rails' strong_params with require, there is a chance the key doesn't exist. When this happens, an ActionController::ParameterMissing exception is thrown.
Ally Piechowski · · 1 min read
In Ruby on Rails, strong_params are a necessity to work with ActionController parameters, but sometimes
an exception of ActionController::ParameterMissing is thrown… Why?
The Code
params.require(:message).permit(:text, :author_id)
The Exception
ActionController::ParameterMissing (param is missing or the value is empty: message)
The Fix
The error is occurring because our params hash does not include the root key
:message. We can diagnose this issue by utilizing params.inspect in either
an interactive console or printing the output of certain commands.
The command I tend to use most to identify mishaps is params.inspect.
For example, the output of this command might be:
<ActionController::Parameters {"messages"=>{"text"=>"My Message", "author_id"=>1337}} permitted: false>
In this case, it appears as though there is a typo in our view! The key was typed
as messages rather than message. The fix is easy, change the view to submit
message instead.
Related Articles
- Solved: Warning: Using the last argument as keyword parameters is deprecated
- How I Audit a Legacy Rails Codebase in the First Week
- Rails default_scope: Why You Should Never Use It
- Rails: How to Use Greater Than/Less Than in Active Record where Statements
- Using Let and Context to Modularize RSpec Tests