I'm a beginner with no programming experience, but I finished Progate's Ruby on Rails course a while back.
When I reviewed it while doing the Rails tutorial, it didn't come out very well when I was doing Progate, but now that I can understand a little more, I would like to summarize what I noticed. I hope it will be useful for those who are progate but have not come to the point and think that they do not understand here.
It's params that appears frequently in the Rails course, but honestly, I thought "I understand, I don't understand ...".
To put it plainly
params is a method to get the value sent from the URL or the value entered in the form with params [: parameter name]
.
2020/6/20 I will add the URL of the page that I referred to very much https://note.com/sakusaku34/n/nc143e418b700
For example, in the case of the detail page of the posted content, it will be used like this.
def show
@post = Post.find_by(id: params[:id])
end
In my case, I understand that each piece of knowledge is being said at that time, but since the knowledge was not connected when I wrote it, I try to break it down into elements as much as possible. The code in the example looks like this.
As a premise, these children are called actions.
The method defined by controller is called an action.
Citation & reference article https://qiita.com/morikuma709/items/5b21e9853c9d6ea70295
@ post
is a variable.
First of all, this wasn't right! (Fatal)@post
and look for the id in the Post table. Search by find_by [id: (id number)]
.
find_by [id: (id number)]
can search the database for one element other than id. (user)params [: id]
corresponds to that id.
This params is a method to get the value sent from the URL or the value entered in the form with params [: parameter name]
. (I wrote it twice because it's important)This code means, "Since the database id of each post was set as the URL in the routing, usefind_by [id: (id number)]
to find and display the one with the same id number." ..
-Params is a method to get the value sent from the URL or the value entered in the form with params [: parameter name]
-Find_by [id: (id number)]
can search the database for one element other than id.
Since I am a beginner, there may be some parts that I am not aware of, but thank you for reading this far. I would appreciate it if you could comment on any corrections or correction points.
Recommended Posts