When using rails g scaffold kittens
the strong parameters function, kitten_params
is
def kitten_params params.fetch(:kitten, {}) end
I am familiar with strong parameters, params.require(:kitten).permit(:name, :age)
but I'm not sure how to use the fetch
method for this.
Strong Parameters, aka Strong Params, are used in many Rails applications to increase the security of data sent through forms. Strong Params allow developers to specify in the controller which parameters are accepted and used.
As you might have guessed, params is an alias for the parameters method. params comes from ActionController::Base, which is accessed by your application via ApplicationController. Specifically, params refers to the parameters being passed to the controller via a GET or POST request.
What Does Parameter (param) Mean? A parameter is a special kind of variable in computer programming language that is used to pass information between functions or procedures. The actual information passed is called an argument.
but I'm not sure how to use the
fetch
method for this
Simple. You do not use fetch
for this. Since you didn't provide any properties when you created the scaffold, rails didn't know what to put into permit
section and generated that code, most sensible for this situation. When you add some fields to your kitten form, upgrade kitten_params
to the normal strong params "shape".
params.require(:kitten).permit(:name, :age)
Accordingly to Documentation, you should just add .permit at the end, like:
params.fetch(:kitten, {}).permit(:name, :age)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With