Ryuichi_natsume
@Ryuichi_natsume

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

フォロー機能のエラー(undefined method `id' for nil:NilClass)解消について

解決したいこと

表題のようにidが渡せず、空のためエラーが出てしまいます。

発生している問題・エラー

ActionView::Template::Error (undefined method `id' for nil:NilClass):
                     6:     <% end %>
                     7:   <% else %>
                     8:     <%= form_for(current_user.relationships.build) do |f| %>
  エラー箇所→  9:       <%= hidden_field_tag :follow_id, user.id %>
                    10:       <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %>
                    11:     <% end %>
                        12:   <% end %>

app/views/relationships/_follow.html.erb:9
app/views/relationships/_follow.html.erb:8
app/views/shared/_header.html.erb:20
app/views/items/index.html.erb:1

 dbdd380ee37f3005c8c32ed745720004.png

該当するソースコード

モデル、マイグレーション

class CreateRelationships < ActiveRecord::Migration[6.0]
  def change
    create_table :relationships do |t|
      t.references :user, foreign_key: true
      t.references :follow, foreign_key: { to_table: :users }

      t.timestamps

      t.index [:user_id, :follow_id], unique: true
    end
  end
end
user.rb

         has_many :relationships
         has_many :followings, through: :relationships, source: :follow
         has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id'
         has_many :followers, through: :reverse_of_relationships, source: :user

         def follow(other_user)
           unless self == other_user
             self.relationships.find_or_create_by(follow_id: other_user.id)
           end
         end

         def unfollow(other_user)
           relationship = self.relationships.find_by(follow_id: other_user.id)
           relationship.destroy if relationship
         end

         def following?(other_user)
           self.followings.include?(other_user)
         end
      end
relationship.rb
class Relationship < ApplicationRecord
  belongs_to :user
  belongs_to :follow, class_name: 'User'

  validates :user_id, presence: true
  validates :follow_id, presence: true
end

コントローラー

relationships_controller.rb
class RelationshipsController < ApplicationController
  before_action :set_user

  def create
    following = current_user.follow(@user)
    if following.save
      flash[:success] = 'ユーザーをフォローしました'
      redirect_to @user
    else
      flash.now[:alert] = 'ユーザーのフォローに失敗しました'
      redirect_to @user
    end
  end

  def destroy
    following = current_user.unfollow(@user)
    if following.destroy
      flash[:success] = 'ユーザーのフォローを解除しました'
      redirect_to @user
    else
      flash.now[:alert] = 'ユーザーのフォロー解除に失敗しました'
      redirect_to @user
    end
  end

  private
  def
    @user = User.find(params[:relationship][:follow_id])
  end

end
items_controller.rb

   def index
    @items = Item.all.order(created_at: :desc)
  end

  def new
    @item = Item.new
  end

  def create
    @item = Item.create(items_params)
    if @item.save
      redirect_to root_path
    else
      render :new
    end
  end

  def show
    @comments = @item.comments.includes(:user)
    @comment = Comment.new
  end

ビュー

_follow.html.erb
<% unless current_user == user %>
  <% if current_user.following?(user) %>
    <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }) do |f| %>
      <%= hidden_field_tag :follow_id, user.id %>
      <%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %>
    <% end %>
  <% else %>
    <%= form_for(current_user.relationships.build) do |f| %>
      <%= hidden_field_tag :follow_id, user.id %>
      <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %>
    <% end %>
  <% end %>
<% end %>
_header.html.erb
略
 <%= render 'relationships/follow', user: @user %>
略

自分で試したこと

・itemscontrollerのindexに@user = User.find(follow_id: params[:user_id])などを記述するも、couldn't find User without an IDのエラーで返されてしまいました。

0

No Answers yet.

Your answer might help someone💌