I understand Ruby on Rails params (with Hanshin Tigers poker faceman, Atsushi Nomi)

About this article

I'm ashamed to say that my understanding of params, which I've only used for some reason, began to be understood from the practical point of view, so a memorandum memo Especially, I used the guy who says params [: user] [: email] in the atmosphere.

What is params in the first place?

** params are called boxes ** that contain parameters. By the way, a parameter is a value sent by a client to a server. In other words, ** params are boxes ** that contain the data sent by the client.

About hashes before explaining params

What is a hash?

It's similar to an array, but the big difference is that it stores data and key combinations. It is difficult to understand what data is converted from an array alone, but it is easy to understand what data is from a hash thanks to the key.

qiita.rb


#If only the key is a hash
#{:Key=> "value"}
{:osaka => "tigers", :tokyo => "giants", :newyork => "yankees"}


##If the key and value are hashes
#{Key: :value}
{osaka: :tigers, tokyo: :giants, newyork: :yankees}

How to retrieve the hash value

=> user = {:name=>"Atsushi Nomi", :email=>"[email protected]"}
>> user[:name]              # :Access the value corresponding to name
=> "Atsushi Nomi"
>> user[:age]          #Access the value corresponding to the undefined key
=> nil

Main subject params

The main subject is finally here. Review about params.

** params are boxes ** that contain the data sent by the client.

What kind of usage have you used?

I've used it somehow params [: user] [: name]

>> params = {}        # 'params'Define a hash (box to put the value)('parameters'Abbreviation for)。
=> {}
>> params[:user] = { name: "Atsushi Nomi", email: "[email protected]" }
=> {:name=>"Atsushi Nomi", :[email protected]"}
>> params
=> {:user=>{:name=>"Atsushi Nomi", :email=>"[email protected]"}}
>>  params[:user][:name]  
=> "Atsushi Nomi"

What's happening with params [: user] [: name]?

① A hash called params is prepared ② In params, the hash {name:" Atsushi Nomi ", email:" [email protected] "} is inserted in the user key. ** That is, there is another hash in the user key of the params hash. ** ** ③ If you want to retrieve the value of the user key of params, you can do it with params [: user]. ④ If you want to retrieve the hash key value contained in the user key value of params, it will be params [: user] [: key name].

params = { user: = { name: "Atsushi Nomi", email: "[email protected]" }}

** params [: parent hash key] [: child hash key] ** I think I can get the value I want.

Dear Nohmisan...(From Murton) Thank you very much, Mr. Nomi, for your success in the Tigers. I respected you so much that I still vividly remember when you hit the glove on the bench, always showing no negative emotions. Please support me even if I decide to play with some team. .. ..

Recommended Posts

I understand Ruby on Rails params (with Hanshin Tigers poker faceman, Atsushi Nomi)
I made a portfolio with Ruby On Rails
I tried installing Ruby on Rails related plugin with vim-plug
Where I was interested in Progate's Ruby on Rails course [params]
I want to add a browsing function with ruby on rails
Understand code coverage with Rspec, the Ruby on Rails test framework
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
[Rails] Procedure for linking databases with Ruby On Rails
Determine the current page with Ruby on Rails
[Ruby on Rails] Upload multiple images with refile
[Ruby on Rails] Delete s3 images with Active Strage
Run Ruby on Rails RSpec tests with GitHub Actions
[Ruby] I can't install puma on Mac [Rails, etc.]
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
Created RSS / Atom format sitemap with Ruby on Rails
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby on Rails] Add a column with a foreign key constraint
[Ruby on Rails] Understand why Set # include? Is so fast
[Ruby on Rails] Implement login function by add_token_to_users with API
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
When I used Slick on Rails, it competed with Turbolinks.
Beginner Ruby on Rails What I learned is being summarized