Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - strong parameters with scaffold - params.fetch

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?

like image 551
Mel Avatar asked Jun 20 '16 02:06

Mel


People also ask

What are rails strong params?

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.

What does params fetch do?

Method: ActionController::Parameters#fetchReturns a parameter for the given key .

What is params [: id in Rails?

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.


1 Answers

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)
like image 56
Deepak Mahakale Avatar answered Oct 14 '22 13:10

Deepak Mahakale