Ruby 2.6.5 Rails 6.0.3.2
When I was studying about Ruby on Rails, it took me a long time to understand params, so I posted it for my own confirmation.
In a word, it is a "box containing information". For example, we always send a "box containing information" when changing pages. Rather, it is possible to transition pages by sending a "box containing information". I think it is difficult to understand by explaining only in words, so I will explain based on a concrete example.
routes.rb
Rails.application.routes.draw do
resources :hoges, only: [:index, :new]
end
ruby:index.html.erb
<%= link_to "Open new page", new_hoge_path %>
ruby:new.html.erb
This is a new page.
hoges_controller.rb
class HogesController < ApplicationController
def index
end
def new
end
end
Suppose you are currently on the index.html.erb page. Then, when I click "Open new page", the following information is sent, the new action of the hoges controller works, new.html.erb opens, and "This is a new page." Is displayed.
{"authenticity_token" => "+ wXNK4Z3C0wrq4AfslPS5zl / 2LSUE6BvV + 23hQpkHryrsVzPb0siDIkarIsNYLK2R502fuXlqQ ==", "commit" => "Open new page", "controller" => "hoges", "action" =
This ** sent information is called params **. Since params is a collection of some information, it is called a "box containing information".
If you take a closer look at params, you can see "controller" => "hoges" and "action" => "new". This allows you to open new.html.erb because it specifies which action on which controller will work.
Please refer to other articles as it will be long if you describe how to check params.
Recommended Posts