I tried to summarize various link_to used this time

Introduction

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.

All link_to methods

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 series

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

I tried to summarize various link_to used this time
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to summarize Java lambda expressions
I tried to summarize the Stream API
What is Docker? I tried to summarize
[Must see !!!] I tried to summarize object orientation!
I tried to organize the cases used in programming
I tried to verify yum-cron
I tried to verify this and that of Spring @ Transactional
I tried to summarize the basic grammar of Ruby briefly
I tried to summarize personally useful apps and development tools (development tools)
I tried to summarize personally useful apps and development tools (Apps)
I tried to summarize object orientation in my own way.
I tried to summarize the words that I often see in docker-compose.yml
I tried to chew C # (indexer)
I tried to summarize what was asked at the site-java edition-
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
I tried to interact with Java
I tried to explain the method
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to understand how the rails method "link_to" is defined
I tried to summarize devise which was difficult at first glance
I tried to summarize the methods of Java String and StringBuilder
Collatz number, Fibonacci number, triangular number I tried to make various sequence programs
I tried to understand nil guard
I tried to chew C # (polymorphism: polymorphism)
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th
I tried to explain Active Hash
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the key points of gRPC design and development
[Introduction to Java] I tried to summarize the knowledge that I think is essential
[After learning Progate] I tried to summarize form_with while comparing with form_tag
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I introduced OpenAPI (Swagger) to Spring Boot (gradle) and tried various settings
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to get started with WebAssembly
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to build AdoptOpenjdk 11 on CentOS 7
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
Amicable numbers, perfect numbers, excess numbers, deficient numbers, palindromic numbers I tried to make various numbers of programs
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to summarize again the devise that was difficult at first sight
[JavaScript] The strongest case when I tried to summarize the parts I do not understand
I tried to convert JavaBean and XML with Jackson formatter XML in this era
I tried using Docker for the first time
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to manage struts configuration with Coggle
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]