Hey Obie! In Rails, you can't require more than one root hash directly using the require
method inside a controller. The require
method ensures that a specific parameter is present, and if it’s not provided, the application throws an error. It's generally used to ensure the main root key is present.
If you want to ensure the presence of multiple keys, a better way to handle it would be using the permit
method or manually checking for the presence of those keys. Here's a quick example:
def user_params
params.require(:user).permit(:name, :email)
end
In this example, the require
method ensures that :user
is present, and the permit
method is used to list the attributes that should be allowed for mass updating.
If you want to ensure that multiple top-level keys are present, you can do it manually:
def validate_params
[:user1, :user2].all? { |key| params.key? key } or raise ActionController::ParameterMissing.new("Missing parameters")
end
Hope this helps! Let me know if you need further clarification or have more questions.
Hey Obie! In Rails, you can't require more than one root hash directly using the `require` method inside a controller. The `require` method ensures that a specific parameter is present, and if it’s not provided, the application throws an error. It's generally used to ensure the main root key is present.
If you want to ensure the presence of multiple keys, a better way to handle it would be using the `permit` method or manually checking for the presence of those keys. Here's a quick example:
```ruby
def user_params
params.require(:user).permit(:name, :email)
end
```
In this example, the `require` method ensures that `:user` is present, and the `permit` method is used to list the attributes that should be allowed for mass updating.
If you want to ensure that multiple top-level keys are present, you can do it manually:
```ruby
def validate_params
[:user1, :user2].all? { |key| params.key? key } or raise ActionController::ParameterMissing.new("Missing parameters")
end
```
Hope this helps! Let me know if you need further clarification or have more questions.
Mike Nichols over 1 year ago