NoMethodError occurred when adding the good function by the following procedure.

--Step1 Model creation-

First of all, create a table.

$rails g model like article: references user: references
$rails db: migrate

Next, add to app/models/user.rb.

class User</pre>
<p><br />
Added to app/models/article.rb.</p>
<pre><code>class Article</pre>
<p><br />
Then add variations to app/model/like.rb.</p>
<pre><code>class Like</pre>
<p><br />
Next, define def already_liked ?.</p>
<pre><code>class User</pre><strong>-Step2 Controller creation-</strong>
<p>First, create a controller.</p>
<pre><code data-language = "Cmd">$rails g controller likes

Next, add the routing.

Rails.application.routes.draw do
#Change before 
  resources: articles
#After change
  resources: articles do
    resources: likes, only: [: create,: destroy]

Change the contents of the Like controller.

class LikesController</pre>
<p><br />
We will add content to the show action in articles_controller.rb.</p>
<pre><code>def show
    @article = Article.find (params [: id])
    @like = Like.new
-Step3 Change view-

Rewrite show.html.erb as follows.

Shikaku Channel  Qualified frequent roads <p><% = @ article.user.email%></p><% = @ article.body%><p><% = @ article.created_at%></p> Number of likes:<% = @ article.likes.count%><% if current_user.already_liked? (@Article)%><% = link_to article_like_path (@article), method:: delete do%>    <i></i><% End%><% else%><% = link_to article_likes_path (@article), method:: post do%>    <i></i><% End%><% end%>Liked users<% @ article.liked_users.each do | user |%><li><% = user.email%></li><% end%><% = link_to "edit", edit_article_path (@ article.id)%><% = link_to "Return to list", articles_path%>

Add the following to index.html.erb.
(NoMethodError occurs on line 13 according to the error message)

Shikaku Channel  Qualified frequent roads  Article list<% = link_to "What is a qualified frequency?", Articles_top_path%><% = link_to "to new post", new_article_path%><% @ articles.each do | t |%>      <% = t.title%><br>  <% = t.content%><br>  <% = t.category%><br>  <% = t.user.email%><br>    <% if current_user.already_liked? (T)%>            <% = link_to article_like_path (id: t.id, article_id: t.id), method :: delete do%>             <i></i><% = t.likes.count%>            <% end%>    <% else%>            <% = link_to article_likes_path (id: t.id, article_id: t.id), method :: post do%>             <i></i><% = t.likes.count%>            <% end%>    <% end%>  <% = t.created_at%><br>  <br>  <% end%>
  • Answer # 1

    It is an error because current_user is nil.

    <% = if current_user.present?&&current_user.already_liked? (T)%>

    If you are using devise, you can use user_signed_in ?.

    The fact that there is no data in current_user means
    Perhaps current_user contains the logged-in user information, so
    I think you're getting an error if you're not logged in.
    If you are not logged in, I think that you are not like it, so I made the above answer.