Search code examples
ruby-on-railscontrollerscaffoldingstrong-parameters

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?


Solution

  • 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)