[Rails] What are params?


Hello, this is @sleepy_cat in charge of the 23 day of the calendar.

I'm a mentor right now, but I feel that this question is quite a lot, and I'll write an article about params that was often seen and difficult to understand when I started learning Rails.

Those who want to read this article

--Model .find (params [: id]) is written in the show action for the time being, but those who do not understand well --Those who do not understand the data sent from the form but think that it will be saved directly in the DB


$ rails -v
$ ruby -v
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-linux]

Rails 5 series. Currently, 6 series is the latest, but I think that the version is not so relevant.

What is params

** A method to get the value (parameter) sent from the client in the URL or form **. If you understand this, you probably haven't read this article from the beginning, so let's take a closer look.

Know how to send parameters

The parameter here means the data (request information) sent together at the time of page transition by the action on the user side. There are three main ways to send. Note) To know this parameter, you need to know about HTTP method and form, but I will omit it because it will be quite long to write it.

Query parameters

I don't know the correct name for URL parameters or query strings ... How to send data when the transition destination is the GET method. The method of sending data is simple, put the data you want to send at the end of the URL and send it. To give an example https://hogehoge/hugahuga/index?like=dog The query parameters after? In this URL. This time you are sending data named like (the content of the data is dog). If you can only send one, you can send many.

In the search function, data is often exchanged using this query. Let's check the URL after actually jumping to the search result page using this qiita search function. You can see that the URL has query parameters.

Another commonly used scene is that it is attached to analyze the access of websites. I think the easiest thing to imagine is the link to amazon or something posted on your personal blog (commonly known as affiliate). That link says that when a product is purchased via that blog, the blog owner will also make a profit, but then there may be a query to show which site it came from.

Route parameters

If the previous query is to put data at the end of ** URL **, this is a way to put parameters inside ** URL ** (it may be more appropriate to consider part of the URL as a parameter) URL).

As an example, consider the following routing.


Rails.application.routes.draw do
  resources :users
   Prefix  Verb    URI Pattern                Controller#Action
    users  GET     /users(.:format)           users#index
           POST    /users(.:format)           users#create
 new_user  GET     /users/new(.:format)       users#new
edit_user  GET     /users/:id/edit(.:format)  users#edit
     user  GET     /users/:id(.:format)       users#show <=I mainly think about this line
           PATCH   /users/:id(.:format)       users#update
           PUT     /users/:id(.:format)       users#update
           DELETE  /users/:id(.:format)       users#destroy

Here, I think that some strange things are mixed in the URL (URI Pattern) of some routing. It is a part such as "/: id". This is exactly the part that becomes the parameter. In this way, the part with a colon in the URL string is not treated as a character but as a kind of variable. (Prerequisite: As the order in which Rails works URL is entered => Matching controller/action is executed by routing) For example, if you try to open a page with the URL / users/1, Rails will determine that the matching routing is/users /: id (.: Format) . It is an image that the data named "id" (the content is 1) is passed and the page transition is performed.


Prefix  Verb    URI Pattern                Controller#Action
  item  GET     /qiita.com/:user_name/items/:item_id(.:format)       items#show

If there is such a path https://qiita.com/sleepy_cat/items/c8eecfc5c486b0f7f2b8 I would like to open this site. What are the parameters you get here?

Answer ① Data named `user_name`, the contents are` sleepy_cat` ② The data `item_id`, the contents are` c8eecfc5c486b0f7f2b8`

Post data

How to send data when the transition destination is POST. I think it is easiest to imagine screen transitions by entering your e-mail address and password in the form when logging in. Normally, the user cannot check the data sent (query etc. can be understood in one shot by checking the URL), so it is the best in terms of security compared to the past.

What is params (again)

Now, I understand that there are several ways to send data. Then how to receive it? I think that those who have read this far are thinking. That's the params method. In Rails, these three data can be combined into one and received in the form of params [: parameter name].

Then there is such a routing

Prefix  Verb    URI Pattern               Controller#Action
  user  GET     /users/:id(.:format)       users#show

The URL is specified as / users/4 and the controller


 def show
  @user = User.find(params[:id])

If it says, you should now know what process and what value is in @ user. ① First, as a URL parameter, data named "id" will be sent to this page. (2) params [: id] receives the sent data "id". ③ Since the content of the received data is "4", it becomes @user = User.find (4). (4) Bring the data with id 4 from the User model with the find method and enter it in @ user.

Let's check

Where should I receive the data that can be received by params? You can check it with a debug gem, but you can also check the terminal.

This is a part of the terminal when the update action is performed with the easily created application, although some changes have been made here.

Started POST "/users/2" for 999.999.999.99 at 2020-12-23 09:25:51 +0000
Cannot render console from 999.999.999.99! Allowed networks:, 
Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ABw1gqVMw8Jhogehogehugahuga",
 "user"=>{"name"=>"sleepy_cat", "text"=>"i love dog"}, "commit"=>"Update User",

Please pay attention to the part of the description that starts with "parameters:". {"Data name" => "Data content", "Data name" => "Data content" ...}. For example, if you do params [: id] here, the data you get is " 2 ". There is also hash data for parameters, " user "=> {" name "=>" sleepy_cat "," text "=>" i love dog "} This part is nested, so when retrieving Is params [: user] [: name] to get " sleepy_cat ".


By the way, the params method can be handled by controller and view. In the case of Rails, whether it is a form or a query, ① page transition ② information gathering in parameters ③ use in controller etc., distribute to model (strong parameters etc. are closely related here, so please check further if you like) If you can draw such a procedure in your head, you may be able to understand the description of Rails more. I hope you read this article and think that you can get along with params a little.

Recommended Posts

[Rails] What are params?
[rails] What are Strong Parameters?
What is params
What is Rails gem devise?
What are command line arguments?
What are practically final variables?
What are Ruby class methods?
config.ru What are you doing?
What are mass assignment vulnerabilities?
What are Java metrics? _Memo_20200818
What is Rails Active Record?
[Rails] What was the error message?
What I learned from studying Rails
[Ruby on Rails] What is Bcrypt?
What are the rules in JUnit?
[Java] What are overrides and overloads?
The problem that the contents of params are completely displayed in the [Rails] view
The identity of params [: id] in rails
bundle install? yarn install? What are you doing
What are the updated features of java 13
[Rails] What is a dot (.) Or a colon (:)?
What are JDK, Oracle JDK, OpenJDK, Java SE?