Since the argument of link_to is nil (null) and an unexpected link was generated, I tried to verify it

Premise

When creating a link with link_to with an instance variable as an argument, a link that I do not remember specifying was generated. What was generated was a link to that page, which apparently happens when you pass nil as an argument. I couldn't find the related page, so I will describe the verification result and the solution. Since we are a beginner, we would appreciate it if you could comment if it is misplaced.

environment

ruby 2.6.5 Ruby On Rails 6.0.0

TL;DR

http://localhost:3000/hoge/When the following description is made in the page 3
A link for that page will be generated as shown below.(Who will use it)
<%= link_to nil,nil %>

=> <a href="/hoge/3">/hoge/3</a>

If there is a possibility that nil will be included in the passed data, if you do the following, it will be blank in the case of nil.
@hoge ・ ・ ・ url data or nil

<%= link_to_if @hoge, @hoge, @hoge %>
If there is a possibility of entering an empty string or space (should that be validated?)
<%= link_to_unless @hoge.blank?, @hoge, @hoge %>

Details (verification and resolution method)

link_to (how to use)
link_to(Link text,path[,option,HTML attribute or event attribute])

If the link text is nil, the content entered in the path will be displayed as the link text.

<%= link_to nil, "https://twitter.com/" %>

=> <a href="https://twitter.com/">https://twitter.com/</a>

If the path is nil (http)://localhost:3000/hoge/(Listed on the page)
A link for the current page is generated

<%= link_to "hoge", nil %>

=> <a href="/hoge/">hoge</a>

So the link text is also nil,If the path also contains nil,
Since the path is nil, the link of the current page is generated
→ Since the link text is nil, it seems that the link of the current page is displayed.

How to solve
How to use
link_to_if(Conditional expression,Link text, url [,option,HTML attribute or event attribute])

@hoge ・ ・ ・ url data or nil

<%= link_to_if @hoge, @hoge, @hoge %>

If you do like ↑@If hoge is nil, nothing is generated, only when there is data.

References

https://railsdoc.com/page/link_to https://railsdoc.com/page/link_to_if The movement when nil is inserted is written in ↓. (Because link_to eventually calls url_for) https://github.com/rails/rails/blob/f33d52c95217212cbacc8d5e44b5a8e3cdc6f5b3/actionview/lib/action_view/routing_url_for.rb#L79

Recommended Posts

Since the argument of link_to is nil (null) and an unexpected link was generated, I tried to verify it
Since the argument of link_to is nil (null) and an unexpected link was generated, I tried to verify it
I tried to display the calendar on the Eclipse console using Java.
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I tried to build the environment little by little using docker
I tried to make it an arbitrary URL using routing nesting
I tried to summarize the basics of kotlin and java
I tried to verify this and that of Spring @ Transactional
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
Since the reading of JdbcCodeList of TERASOLUNA is slow, I tried to register multiple at once.
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
Since the Rspec command is troublesome, I tried to make it possible to execute Rspec with one Rake command
I tried to understand how the rails method "link_to" is defined
I tried to summarize the methods of Java String and StringBuilder
I tried to summarize the key points of gRPC design and development
Get the type of an array element to determine if it is an array
I want to pass the argument of Annotation and the argument of the calling method to aspect
I tried to measure and compare the speed of GraalVM with JMH
Since the du command used when the capacity is full is difficult to use, I tried wrapping it with ruby
Since the image of the lock screen of Windows 10 is beautiful, I wanted to make it a slide show of wallpaper
I tried to link grafana and postgres [docker-compose]
Sazae-san's rock-paper-scissors I tried to verify the theoretical value and the measured value of the probability of the same hand 5 consecutive times with Ruby
[Swift] If the support of the application is iOS 11 or later, it was not necessary to use Int and Int64 properly
[Rails] I want to display the link destination of link_to in a separate tab
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
[Memo] MyBatis's mysterious movement, set null to the element of the list object and return it.
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
[Swift] I tried to implement the function of the vending machine
I tried to summarize the basic grammar of Ruby briefly
I was addicted to the setting of laradock + VSCode + xdebug
I tried to build the environment of WSL2 + Docker + VSCode
I tried to make it possible to set the delay for the UDP client of Android by myself
I tried to express the result of before and after of Date class with a number line
I want to make the frame of the text box red when there is an input error