Search code examples
ruby-on-railsstrong-parameters

Rails 4 - controller strong params - generated with scaffold


I am trying to make an app in Rails 4.

I use scaffolding generators to make my resources starting points.

I'm noticing, when I ask questions on this board, that people comment on the form of my strong params definitions in the controllers.

The scaffold generator creates them this format:

def industry_params
      params[:industry].permit(:sector, :icon)
end

Most resources that show how to whitelist strong params, show this format.

def industry_params
  params.require(:industry).permit(:sector, :icon)
end

http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

Is there anything wrong with the way the rails scaffold generator creates this method?


Solution

  • The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.

    The permit method returns a copy of the parameters object, returning only the permitted keys and values.

    As you can see while using scafffold's default system we need to check for is there is any value in the params, whereas require throw an error if its missing.