Rails 4 - strong options with scaffold - params.fetch

I use scaffold commands to make my components in my Rails 4 application.

Recently, the terminology used in the method to set strong parameters has changed from params.require to params.fetch, and now there are curly braces in the setting.

private # Never trust parameters from the scary internet, only allow the white list through. def engagement_params params.fetch(:engagement, {}) end 

I can not find documentation explaining the change or its use.

Can I write params.fetch (: engagement) .permit (: opinion) to the fetch command? I do not know what to do with curly braces.

How to fill in strong parameters using this new form of expression?

+6
source share
1 answer

I never came across this situation, but here I found a link to the fetch method

http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-fetch

Is it possible to write the params.fetch (: engagement) .permit (: opinion) command to the fetch command?

Yes, you can still use

 params.fetch(:engagement).permit(:attributes, :you, :want, :to, :allow) 

I do not know what to do with curly braces.

This is the default value that will be returned if the key is missing or it throws an error

 params.fetch(:engagement) #=> *** ActionController::ParameterMissing Exception: param is missing or the value is empty: engagement params.fetch(:engagement, {}) #=> {} params.fetch(:engagement, 'Francesco') #=> 'Francesco' 

How to fill in strong parameters with this new expression form?

 params.fetch(:engagement).permit(:attributes, :you, :want, :to, :allow) 
+4
source

All Articles