I use scaffold commands to make my components in my Rails 4 app.
Recently, the terminology used in the method to set the strong params has changed from params.require to params.fetch and now there are curly braces in the setup.
private
# Never trust parameters from the scary internet, only allow the white list through.
def engagement_params
params.fetch(:engagement, {})
end
I can't find any documentation explaining the change or how to use it.
Can I still write params.fetch(:engagement).permit(:opinion) into the fetch command? I don't know what to do with the curly braces.
How do I complete the strong params using this new form of expression?
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.
Method: ActionController::Parameters#fetchReturns a parameter for the given key .
params[:id] is meant to be the string that uniquely identifies a (RESTful) resource within your Rails application. It is found in the URL after the resource's name.
I never came across this situation but here, I found the reference to fetch
method
http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-fetch
Can I still write params.fetch(:engagement).permit(:opinion) into the fetch command?
Yes, you can still use
params.fetch(:engagement).permit(:attributes, :you, :want, :to, :allow)
I don't know what to do with the curly braces.
It's a default value which will be returned if key is not present or it will throw 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 do I complete the strong params using this new form of expression?
params.fetch(:engagement).permit(:attributes, :you, :want, :to, :allow)
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