# Understanding Rails Parameters

*Published on March 05, 2025*

Tags: Controllers

---


> 💡 To serve your users, you have to know what they asked for.

If you are building a dynamic web application, you will need to access and process the data submitted by the user on the server. This data could arrive in a number of ways:

- the user might upload a photo,
- write a comment,
- post a tweet,
- submit a form, or
- click a link to an article they want to read.

Regardless of the way by which the data is sent, you want to read this data on the server to do something useful, such as saving that photo, storing the comment / tweet in the database, access the form data, or find the article from the database.

**The big question is: How can you access this data on the server, in your Rails application?**

**The answer is Rails parameters.**&nbsp;They let you access the user-submitted data in your Rails application, specifically in your Rails controllers.

> Similar to the function parameters, Rails parameters read the data submitted by the user, either in the link or via form submission and make it available in your Rails application in the&nbsp;`params`&nbsp;object.

The&nbsp;`params`&nbsp;object serves as a hash-like list of key-value pairs. Each key corresponds to a named input field from the request, and the value contains the data provided by the user for that input.

For example, if your user submits a form with fields named&nbsp;`first_name`&nbsp;and&nbsp;`last_name`, the controller can access these fields as follows:

```ruby
first_name = params[:first_name]
last_name = params[:last_name]
```

**Remember:**&nbsp;Since the&nbsp;`params`&nbsp;object is built from the user input, it's not to be trusted, and you should validate and sanitize before using the data in your application. Rails provides various ways to do this, and we'll explore them all in future posts.

## A Simple Analogy to Understand Params

As we learned above, the browser sends a request to the Rails app (on the server), it can include data in the request, which is accessed via the&nbsp;`params`&nbsp;object. We can illustrate this interaction with a simple analogy. Consider the following code that mimics the browser-server interaction.

```ruby
# Rails app
def rails(params)
  id = params[:id]
  name = params[:name]
end

# browser sends the parameters to Rails app
rails(id: 5, name: 'learn-to-program')
```

In this example, the&nbsp;`rails`&nbsp;function plays the role of your Ruby on Rails application. The browser sends a request to the app by calling this function, passing some data. The Rails app makes this data available in a Hash-like object, which is called, not surprisingly,&nbsp;**params,**&nbsp;which stands for parameters.

Hope that gave you an conceptual understanding of parameters in Rails.

## Different Ways Browsers Send Data to Server

There are two ways in which the browser can send data to the Rails application: via query strings in the URL (GET) and by submitting a form (POST).

**Query Strings**

- Include the data in the URL after the&nbsp;`?`, e.g.&nbsp;`/posts?id=3&user=ak`
- Multiple pieces of key-value pairs are separated by&nbsp;`&`
- Typically submitted in a GET request, e.g. clicking a link

**Form Submission**

- Contains the data entered into a form by the user.
- Typically posted in a POST request, e.g. submitting a form

The important thing to know is that&nbsp;**Rails doesn’t care about the request type**&nbsp;(`GET`&nbsp;or&nbsp;`POST`) when accessing the data on the server.

> Rails won’t distinguish between the information it receives from query strings and the data from a form submission. It groups all the data into the&nbsp;`params`&nbsp;hash, which is available in the controller.

Let's inspect both approaches in detail.

## **Query Strings**

Let's assume you've got this route:

```ruby
Rails.application.routes.draw do
  get 'posts/sample', to: "posts#sample"
end
```

This route dispatches the request made to the&nbsp;`posts/sample`&nbsp;URL endpoint to the&nbsp;`sample`&nbsp;action method on the&nbsp;`PostsController`&nbsp;class.

When you visit the URL&nbsp;`http://localhost:3000/posts/sample?id=3&user=ak`, Rails makes the query data available in the following&nbsp;`params`&nbsp;hash:

```ruby
{
  "id"         => "3", 
  "user"       => "ak", 
  "controller" => "posts", 
  "action"     => "sample"
}
```

Note that the&nbsp;`params`&nbsp;hash also includes the controller and action names. Though you can access them both in your controller, Rails recommends using the&nbsp;[`controller_name`](https://api.rubyonrails.org/v7.0.3/classes/ActionController/Metal.html#method-i-controller_name)&nbsp;and&nbsp;[`action_name`](https://api.rubyonrails.org/v7.0.3/classes/AbstractController/Base.html#method-i-action_name)&nbsp;methods to access these values.

> 💡 Remember to use the&nbsp;`controller_name`&nbsp;and&nbsp;`action_name`&nbsp;methods on the controller to access the controller and action names respectively, instead of reading them directly from the params object.

The&nbsp;`params`&nbsp;object is in fact, an instance of&nbsp;`ActionController::Parameters `class, that acts like a hash.&nbsp;**We can assume it’s a hash for all practical purposes.**

```ruby
#<ActionController::Parameters {"id"=>"3", "user"=>"ak", "controller"=>"posts", "action"=>"sample"} permitted: false>
```

The&nbsp;`permitted`&nbsp;property is related to strong parameters. To learn more about them, check out the following article: [Why You Need Strong Parameters in Rails](/why-use-strong-parameters-in-rails)

That's enough about query parameters. Now let's learn the second way the users can send data to the server: by submitting forms.

## **Form Submission**

Consider this form which makes a&nbsp;`POST`&nbsp;request on the&nbsp;`/posts/upload`&nbsp;endpoint.

```html
<form action="posts/upload" method="post">
  <div>
    <label for="name">Name:</label>
    <input type="text" id="name" name="user_name">
  </div><br>

  <div>
    <label for="mail">E-mail:</label>
    <input type="email" id="mail" name="user_email">
  </div><br>

  <button type="submit">Submit</button>
</form>
```

When you fill out and submit the form, the browser sends the data to your Rails application, which creates the following&nbsp;`params`&nbsp;hash:

```ruby
{
  "user_name"  => "Akshay", 
  "user_email" => "ak@email.com", 
  "controller" => "posts",
  "action"     => "build"
}
```

Notice that the keys on the&nbsp;`params`&nbsp;hash are the same values I used for the&nbsp;`name`&nbsp;attributes on the form. Rails extracts these&nbsp;`name`&nbsp;values from the submitted data and adds them as keys to the&nbsp;`params`&nbsp;hash.

## **Dynamic Parameters**

Parameters can be specified as part of the route declaration itself. If your route URL contains a symbol, Rails will use that symbol name as the key in the&nbsp;`params`&nbsp;hash.

The following route instructs Rails to map the URL parameter to&nbsp;`title`.

```ruby
# routes.rb
get 'posts/lookup/:title', to: 'posts#lookup'
```

If you visit the&nbsp;`posts/lookup/learn-to-code`&nbsp;URL, Rails will extract the&nbsp;`learn-to-code`&nbsp;string and assign it to the&nbsp;`title`&nbsp;key on the&nbsp;`params`&nbsp;hash.

```ruby
# params = { "title" => "learn-to-code" }

class PostsController
  def lookup
    title = params[:title]  # learn-to-code
  end
end
```

To learn more about the Rails router, check out the following article: [Understanding the Rails Router: Why, What, and How](/understanding-rails-router-why-what-how)

More information can be found in my Rails Router handbook: [The Rails Router](/tag/router)

## Nested Parameters

Instead of sending the&nbsp;`name`&nbsp;values as keys directly, you can group them under a common key. For example, to group the&nbsp;`user_name`&nbsp;and&nbsp;`user_email`&nbsp;under the&nbsp;`client`&nbsp;key, change the&nbsp;`name`&nbsp;attribute's value as follows:

```html
<form action="upload" method="post">
  <div>
    <label for="name">Name:</label>
    <input type="text" id="name" name="client[user_name]">
  </div><br>

  <div>
    <label for="mail">E-mail:</label>
    <input type="email" id="mail" name="client[user_email]">
  </div><br>

  <button type="submit">Submit</button>
</form>
```

This will create the following&nbsp;`params`&nbsp;hash on the server:

```ruby
{
  "client": {
    "user_name": "Akshay",
    "user_email": "ak@email.com"
  },
  "controller": "posts",
  "action": "build"
}
```

Notice that Rails has grouped the&nbsp;`user_name`&nbsp;and&nbsp;`user_email`&nbsp;under the&nbsp;`client`. The nice thing is, when you use the Rails&nbsp;`form_with`&nbsp;helper, Rails takes care of this for you. For example, consider this form:

```html
<%= form_with model: @book do |form| %>
  <div>
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>
  <div>
    <%= form.label :author %>
    <%= form.text_field :author %>
  </div>
  <%= form.submit %>
<% end %>
```

It generates the following HTML:

```html
<form action="/books" method="post">
  <div>
    <label for="book_title">Title</label>
    <input type="text" name="book[title]" id="book_title">
  </div>
  
  <div>
    <label for="book_author">Author</label>
    <input type="text" name="book[author]" id="book_author">
  </div>
  
  <input type="submit" name="commit" value="Create Book">
</form>
```

In the controller, you can access the title and author values as follows:

```ruby
title = params[:book][:title]
author = params[:book][:author]
```

It's pretty remarkable how much convenient Rails makes things for you. Recently, I was working on a non-Rails project, and was struck by how much more manual effort was required. Readers, if you're working on Rails apps (and getting paid for it), count your blessings.

## **Sending JSON**

For Rails applications that expose an API, you can receive JSON data from the client to the application. Rails will load it into the&nbsp;`params`&nbsp;hash, and you can access it in the controllers.

For example, if the client sends the following JSON data:

```json
{ "company": { "name": "Apple", "address": "Cupertino" } }
```

The&nbsp;`params`&nbsp;hash will be

```ruby
{ :company => { "name" => "Apple", "address" => "Cupertino" } }
```

Pretty convenient.

## Sending Arrays and Hashes as Parameters

If you're using named route helpers, you can pass arrays as follows:

```ruby
get dashboard_path(frameworks: [ "Ruby on Rails", "Laravel" ])

params[:frameworks] # [ "Ruby on Rails", "Laravel" ]
```

Otherwise, you can pass the following URL and Rails will parse it to build the array parameters:

```text
http://www.example.com?frameworks[]=Ruby+on+Rails&frameworks[]=Laravel
```

Similarly, you can pass a hash as follows:

```ruby
get home_path(language: { name: "Ruby", creator: "Matz" })

params[:language][:name]     # "Ruby"
params[:language][:creator]  # "Matz"
```

The above helper generates the following URL:

```text
http://www.example.com/?language[creator]=Matz&language[name]=Ruby
```

## **How to Pass Parameters when Redirecting**

When redirecting to another route, you can pass the parameters by passing them as hash parameters to the&nbsp;`redirect_to`&nbsp;method. For example,

```ruby
redirect_to controller: 'articles', action: 'show', id: 3, user: 'ak'
```

The&nbsp;`ArticlesController`&nbsp;can access the passed values using the&nbsp;`params`&nbsp;hash as usual. Also remember that you can’t redirect a&nbsp;`POST`&nbsp;request.

I hope that you have a pretty good understanding of how parameters work in Rails by now.&nbsp;**To summarize what we’ve learned so far:**

- All the data sent by the browser is available in the&nbsp;`params`&nbsp;hash.
- Rails won't differentiate between the data that comes from a GET request (query string parameters) vs. the data that comes from a POST request (form submission).
- You can pass nested data using special syntax in your forms and Rails will make it available as a nested Hash.

In the next post in the series, we'll explore about the mass assignment vulnerability and how Rails prevents it with the concept of Strong Parameters. Stay tuned!

