Params Wrapper in Ruby on Rails Explained

Auto wrap parameters in a nested hash to simplify requests

Chris I.
Chris I.
Sep 29, 2020 · 2 min read
Image for post
Image for post
Photo by Burst from Pexels

If you’ve ever wondered why sending this in your request.

Becomes this in your controller params.

It’s because of ParamsWrapper.

Note how “name” and “location” exist twice, once inside “meetup” and once outside.

A Quick Demonstration

Let’s create a simple Rails app and review how params changes with and without ParamsWrapper.

Create a new Rails app on the command line.

Add a controller called /controllers/meetups_controller.rb, and paste in the following.

Edit /config/routes.rb.

And make a request from the command line.

The Rails server logs will now display below.

We didn’t specify a “meetup” key/value in the request, so how did it get there?

ActiveRecord’s ParamsWrapper did that automatically. It’s default behavior is to wrap requests in a nested hash, based on the name of the resource.

Turn ParamsWrapper Off

The default is set in /initializers/wrap_parameters.rb.

But it can be turned off by changing [:json] to [] in the initializer, or inside a specific controller.

After deactivating, controller params look like this.

The Benefit of ParamsWrapper

It allows you to send requests like this.

Instead of this.

Strong params (see below), requires the root element “meetup”, so this makes it slightly easier on the requester.

That said, the net benefit is debatable given the ambiguity it adds, but that’s Rails for you :)

I hope you found this useful. Let me know if you have related questions!

Rails 6.0.3.2 was used for the above code.

Ruby Daily

Diving into Ruby and Rails

Chris I.

Written by

Chris I.

Full Stack Dev. Data Scientist. Ruby, Python, JavaScript, AWS. Toronto. Ruby/Rails mentor/consultant for hire. Contact: chris.i.the.data.guy “at” gmail.com

Ruby Daily

Articles on Ruby, Rails and software development.

Chris I.

Written by

Chris I.

Full Stack Dev. Data Scientist. Ruby, Python, JavaScript, AWS. Toronto. Ruby/Rails mentor/consultant for hire. Contact: chris.i.the.data.guy “at” gmail.com

Ruby Daily

Articles on Ruby, Rails and software development.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store