A story that deepened the understanding of devise's methods user_signed_in? And current_user through error resolution

Development environment

Mac OS Catalina 10.15.7 ruby 2.6 series rails 6.0 series

The beginning of the matter

It was one day when I was making an original app as a portfolio for changing jobs. When a non-logged-in user makes a request to transition to the details page locally, the following error occurs

スクリーンショット 2021-01-18 20.02.57.png

Solution

From the conclusion, it was solved by adding the condition of user_signed_in? &&.

Code at the time of error

ruby:show.html.erb


<% if current_user.id == @post.user.id %>
  <div class = "btn-contents">
    <%= link_to 'To edit', edit_post_path(@post.id), class: "edit-btn"%>
    <%= link_to 'delete', "#", class: "delete-btn"%>
  </div>
<% end %>

Code after error resolution

ruby:show.html.erb


<% if user_signed_in? && current_user.id == @post.user.id %>
  <div class = "btn-contents">
    <%= link_to 'To edit', edit_post_path(@post.id), class: "edit-btn"%>
    <%= link_to 'delete', "#", class: "delete-btn"%>
  </div>
<% end %>

It was a very dull story.

Cause

The cause was that the current_user method was also used when requesting a non-logged-in user because I forgot the conditions for the non-logged-in user.

current_user is a method that can get the information of the user who is currently logged in, but in this case, I tried to get the login information even though I was not logged in.

Therefore, it is said that rails teacher got angry with a feeling like "current_user ??? I don't have such information because I haven't logged in in the first place ???".

So, first of all, by using the user_signed_in? Method in the previous condition, false will be returned if you are not logged in. That way, if you haven't logged in, you won't be able to transition to the following conditions, and the error will be resolved. (The description A && B does not refer to B's condition if A's condition is false)

Countermeasures

As a countermeasure, I think that "somehow stop understanding the method".

The cause of the error is as described above, but I think the root cause is "I used the method with the intention of somehow remembering and understanding it." I'll be careful,,,

Recommended Posts

A story that deepened the understanding of devise's methods user_signed_in? And current_user through error resolution
A story that struggled with the introduction of Web Apple Pay
The story that Tomcat suffered from a timeout error in Eclipse
A story that confirmed the profile of Yasuko Sawaguchi 36 years ago
This and that of the JDK
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
Ubuntu 20.04 The story of creating CFn that installs CloudWatch agent on LTS and creates a configuration file
A collection of phrases that impresses the "different feeling" of Java and JavaScript
The story of forgetting to close a file in Java and failing
Deepened my understanding of the merge method
A story that separates business logic and model
The story that docker had a hard time
A story about making a Builder that inherits the Builder