This time I summarized params. Actually, the reason for summarizing this time was a question from a beginner, params. I ran away to params so I couldn't answer. I found out when I read the article on prams again this time, so I wrote it as a reference. I am also a beginner
params are for putting the information obtained by the link or input form into the method. This time I will go to my page.
show.html.erb
<a href = "users/current_user.id" class = "users">Yamada Taro</>
id | name | age |
---|---|---|
1 | Yamada Taro | 22 |
a.controller.rb
def show
@user = User.find(params[:id])
binding.pry
end
pry(#<InfomationsController>)> params
=> <ActionController::Parameters {"controller"=>"infomations", "action"=>"show", "id"=>"1"} permitted: false>
By doing this, the information of the current_user.id number is entered in @user by adding params to the id. On the contrary, if there are no params
a.controller.rb
def show
@user = User.find(:id)
Couldn't find User with 'id'=id)
I get an error saying that the id cannot be recognized. So you should get the data using params except for the index method.
Recommended Posts