I want to implement a follow function. ruby 2.5.1p57 Rails 5.2.4.2
The following error message occurred while implementing the follow function.
error
undefined method `id' for nil:NilClass
relationships table
class CreateRelationships < ActiveRecord::Migration[5.2]
def change
create_table :relationships do |t|
t.integer :follower_id, foreign_key: true
t.integer :following_id, foreign_key: true
t.timestamps null: false
end
add_index :relationships, :follower_id
add_index :relationships, :following_id
add_index :relationships, [:follower_id, :following_id], unique: true
end
end
relationship.rb
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :following, class_name: "User"
validates :follower_id, presence: true
validates :following_id, presence: true
end
user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, dependent: :destroy
has_many :likes, dependent: :destroy
has_many :liked_posts, through: :likes, source: :post
has_many :comments
def already_liked?(post)
self.likes.exists?(post_id: post.id)
end
has_many :following_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :followings, through: :following_relationships, source: :following
has_many :follower_relationships, class_name: "Relationship", foreign_key: "following_id", dependent: :destroy
has_many :followers, through: :follower_relationships, source: :follower
def following?(user)
following_relationships.find_by(following_id: user.id)
end
def follow!(other_user)
following_relationships.create!(following_id: user.id)
end
def unfollow!(other_user)
following_relationships.find_by(following_id: user.id).destroy
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users
root to: "posts#index"
resources :posts do
resources :comments, only: :create
resources :likes, only: [:create, :destroy]
end
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
end
ruby:_follow_form.html.haml
- unless current_user?(@user)
#follow_form
- if current_user.following?(@user)
= render 'unfollow'
- else
= render 'follow'
users_controller.rb
class UsersController < ApplicationController
def show
end
def edit
end
def update
if current_user.update(user_params)
redirect_to root_path
else
render :edit
end
end
def following
@user = User.find(id: params[:id])
binding.pry
@users = @user.followings
render 'show_following'
end
def followers
@user = User.find(id: params[:id])
binding.pry
@users = @user.followers
render 'show_follower'
end
private
def user_params
params.require(:user).permit(:id, :name, :email)
end
end
-It was confirmed that the association of user.rb (user model) was set up correctly. -Checked all over for spelling mistakes in user.rb (user model) and _follow_form.html.haml (partial template).
error
following_relationships.find_by(following_id: user.id)
undefined method `id' for nil:NilClass
-I tried to call the id method for nil, but I couldn't find it. ・ User is nil
→ What causes user to become nil? ?? ??
-@User was not defined in the show action of users_controller.rb.
The error screen says NoMethodError in Users # show
, so I should be suspicious of the show action in users_controller.rb.
I solved it by defining @user as follows in the show action of users_controller.rb.
users_controller.rb
def show
@user = User.find(params[:id])
end
With params [: id]
, the value sent with the path is received from the screen before the transition.
I found it on a site called teratail, an engineer's yahoo wisdom bag. [Ruby on Rails] Follow function undefined method ʻid'for nil: NilClass error message appears
Recommended Posts