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.
** 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.
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}
=> 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
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?
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"
① 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