How to use params.fetch strong parameters

Viewed 22498

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.

3 Answers

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

params.fetch(:kitten, {}).permit(:name, :age)

According to documentation, you can't use .require when you don't have an instance of an object.
Then .fetch supplies a default params for your uncreated object (#new and #create actions).

Related