The most stumbling block in this curriculum was the link_to method
. Of course, I think I understand the basic usage, but the argument of path doesn't seem to fit.
This post is not meant to explain anything, but a record for you to read the code. Below, all the link_to of the application created this time is cut off the unnecessary description and posted with how to read the arguments.
①
ruby:application.html.erb
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<%= link_to "New post", new_prototype_path %>
Since both "logout" and "new post" displayed in the signed-in state are pages and processes common to everyone, there is no need to pass an argument in ().
The logout page does not differ from user to user, and the new post page does not differ from user to user.
Since the HTTP method of link_to method
is GET by default, specify the method in the third argument when logging out.
②
ruby:application.html.erb
<%= link_to "Login", new_user_session_path %>
<%= link_to "sign up", new_user_registration_path %>
The idea is the same as ①. This time it is displayed when you are not logged in. Since the login screen and new registration do not change depending on the user, there is no need for an argument.
③
ruby:application.html.erb
<%= link_to image_tag("logo.png "), root_path %>
root_path does not need to have any arguments!
④
ruby:prototypes/show.html.erb
<%= link_to "To edit", edit_prototype_path(@prototype) %>
<%= link_to "delete", prototype_path(@prototype), method: :delete %>
The part to edit or delete from the details page of what you posted.
Since one record is selected as @ prototype = Prototype.find (params [: id])
in the show action, it is possible to determine which prototype to edit / delete.
⑤
ruby:prototypes/_prototype.html.erb
<%= link_to prototype.title, prototype_path(prototype.id) %>
<%= link_to image_tag(prototype.image), prototype_path(prototype.id) %>
user_path jumps to the user's My Page. Arguments are needed to clarify who my page is.
ruby:prototypes/index.html.erb
<%= link_to current_user.name + "Mr.", user_path(current_user) %>
Clicking on the logged-in user name will take you to that user's My Page.
Current_user
that can be used by using devise's Gem.
ruby:prototypes/_prototype.html.erb
<%= link_to "by " + prototype.user.name, user_path(prototype.user.id) %>
Get the id of the user associated with prototype.
ruby:prototypes/show.html.erb
<%= link_to "by " + @prototype.user.name, user_path(@prototype.user.id) %>
The code of the part where the name is displayed and who posted it.
In the show action, @ prototype = Prototype.find (params [: id])
and one record are selected, so the id of the user associated with prototype can be obtained.
ruby:prototypes/show.html.erb
<%= link_to comment.user.name, user_path(comment.user.id) %>
The code where the comment is displayed. In each statement|For block parameters (variables)comment
|Enters and gets the id of the user who is associated with comment.
Recommended Posts