Search code examples
rubyruby-on-rails-4

How can I view list of permitted params using Rails strong_parameters?


I'm in the process of migrating from a Rails 3 application up to Rails 4 and making the changes to strong parameters. During this process I've been doing some command line debugging and would like to be able to view of list of all currently permitted parameters.

I was thinking this would work something like this.

params.permitted?

I've looked at the docs and googled, but have come up empty.


Solution

  • No, that is not possible. Because params.require(:foo).permit(:bar) does not define a list of allowed parameters, but filters the params parameter hash at each request with the attribute names as arguments.

    When you want to see the list of permitted parameters, then you have to take a look at your controller's source code.

    When you want to use the list of permitted parameter names programmatically, then I suggest storing the allowed parameters in a constant which can be read from everywhere in your application, like this:

    # in the foos_controller.rb
    ALLOWED_PARAMS = [:bar]
    
    def foo_params
      params.require(:foo).permit(*ALLOWED_PARAMS)
    end
    
    # Elsewhere in the application or in the Rails console
    FoosController::ALLOWED_PARAMS
    #=> [:bar]
    

    Read more about how this parameter filtering works in the Rails docs.