Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use params.fetch strong parameters

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.

like image 307
Andy Avatar asked Aug 15 '17 15:08

Andy


People also ask

What are strong params and why do we use them?

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.

How does params work in Rails?

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 are @params?

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.


2 Answers

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) 
like image 130
Sergio Tulentsev Avatar answered Oct 03 '22 19:10

Sergio Tulentsev


Accordingly to Documentation, you should just add .permit at the end, like:

params.fetch(:kitten, {}).permit(:name, :age) 
like image 22
Fernando Oléa Avatar answered Oct 03 '22 19:10

Fernando Oléa