9

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?

Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

8

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)
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
  • Thanks @Deepak - what purpose do the curly braces serve in the scaffold? – Mel Jun 20 '16 at 08:02
  • 2
    @Mel It will return you empty hash in case there no key :engagement in params. check above I have edited the answer – Deepak Mahakale Jun 20 '16 at 08:36
  • Thanks @Deepak - does that mean if I want to start using the new way of setting strong params, I should use the third example in your answer? – Mel Jun 20 '16 at 08:52
  • @Mel Well you can but go through this one and decide if it suits you http://guides.rubyonrails.org/action_controller_overview.html#more-examples – Deepak Mahakale Jun 20 '16 at 09:26