Following users on Ruby on Rails
<p>I decided to upgrade code of  <a href="https://www.railstutorial.org/book" rel="nofollow">https://www.railstutorial.org/book</a>  chapter 14 Following users. My code does not work, when <code>current_user</code> goes on another page and open following. <code>current_user</code>  sees himself and want to unfollow like original instagram. unfollow button does not work</p>

<p><strong>show_follow.html.erb:</strong></p>

<pre><code>&lt;div class="line-gray-modals"&gt;&lt;/div&gt;
  &lt;% if @users.any? %&gt;
    &lt;% @users.each do |user| %&gt;
  &lt;ul class="follow-list"&gt;
    &lt;li&gt;&lt;%= image_tag(user.avatar.url(:thumb), :class =&gt; "img-cirkle") %&gt;&lt;/li&gt;
    &lt;li &gt;
      &lt;div class="modal-follow-ns" &gt; &lt;div class="bold"&gt; &lt;%= link_to user.username, user %&gt;&lt;/div&gt; &lt;br&gt;
      &lt;div class="modal-follow-ns2"&gt;&lt;%= link_to truncate(user.status, :length =&gt; 50) %&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;/li&gt;
    &lt;li class="button-modal"&gt;
      &lt;% unless user == current_user %&gt;
        &lt;div id="follow_form"&gt;
          &lt;% if current_user.following?(user) %&gt;
            &lt;%= form_for(current_user.relationships.find_by(followed_id: user),
                           html: { method: :delete },
                           remote: true) do |f| %&gt;
              &lt;%= f.submit "Unfollow", class: "btn btn-large" %&gt;
            &lt;% end %&gt;
          &lt;% else %&gt;
            &lt;%= form_for(current_user.relationships.build(followed_id: user.id),
                                                         remote: true) do |f| %&gt;
              &lt;div&gt;&lt;%= f.hidden_field :followed_id %&gt;&lt;/div&gt;
              &lt;%= f.submit "Follow", class: "btn btn-large btn-primary" %&gt;
            &lt;% end %&gt;
          &lt;% end %&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
      &lt;% if user == current_user %&gt;
        &lt;%= form_for(@user.relationships.find_by(followed_id: user),
                                          html: { method: :delete },
                                            remote: true) do |f| %&gt;
          &lt;%= f.submit "Unfollow", class: "btn btn-large" %&gt;
        &lt;% end %&gt;
      &lt;% end %&gt;
    &lt;/li&gt;
    &lt;%= will_paginate %&gt;
    &lt;li&gt;  &lt;div class="line-gray-modals"&gt;&lt;/div&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;% end %&gt;
</code></pre>

  

<p><strong>relationships_controller.rb:</strong></p>

<pre><code>   def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end
</code></pre>

<p><strong>Terminal:</strong></p>

<pre><code>Completed 500 Internal Server Error in 10ms (ActiveRecord: 1.9ms)
NoMethodError (undefined method `destroy!' for nil:NilClass):
app/models/user.rb:28:in `unfollow!'
app/controllers/relationships_controller.rb:15:in `destroy'
</code></pre>