Ask A Question

Notifications

You’re not receiving notifications from this thread.

error when i click on follow button ... Relationship is not working

khemlall mangal asked in Rails

All i am trying to create follow and follwers relationship and i am experiencing some issues. Chris i think you should create a course on this as well. i am getting issue undefined method `id' for nil:NilClass

Secondly i created a follow button class and that not being displayed on the page when i render it

Relationship.rb

class Relationship < ActiveRecord::Base

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, :presence => true
  validates :followed_id, :presence => true

  validate :validate_followers

  def validate_followers
    errors.add(:follower_id, "You cannot follow yourself") if follower_id == followed_id
  end
end

User.rb

class User < ActiveRecord::Base
 mount_uploader :avatar, AvatarUploader
  mount_uploader :campaignimage, ImageUploader

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
acts_as_follower
acts_as_followable

         # User Avatar Validation
  validates_integrity_of  :avatar
  validates_processing_of :avatar
 validates_integrity_of  :campaignimage
  validates_processing_of :campaignimage
   validates :first_name,:last_name,  presence: true  



    has_many :createcampaigns, dependent: :destroy
    has_many :relationships, foreign_key: "follower_id", dependent: :destroy
    has_many :squeals, dependent: :destroy
    has_many :tweets
    has_many :comments, as: :commentable
    has_many:reviews, dependent: :destroy
    has_many:createcampaign, dependent: :destroy
    has_many :posts, dependent: :destroy

     has_many :posts, dependent: :destroy # remove a user's posts if his account is deleted.
         has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
         has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy

         has_many :following, through: :active_relationships, source: :followed
         has_many :followers, through: :passive_relationships, source: :follower





     # follow another user
         def follow(other)
           active_relationships.create(followed_id: other.id)
         end

         # unfollow a user
         def unfollow(other)
           active_relationships.find_by(followed_id: other.id).destroy
         end

         # is following a user?
         def following?(other)
           following.include?(other)
         end



   private
    def avatar_size_validation
      errors[:avatar] << "should be less than 500KB" if avatar.size > 0.5.megabytes
    end

end

Users.rb

devise_for :users 
  resources :users do

    member do
     get :following, :followers
    end
    resources :createcampaigns
  resources :comments,module: :user
  end

user controller

class UsersController < ApplicationController
 before_action :authenticate_user,
                only: [:index, :edit, :update, :destroy, :following, :followers]

  def show

    if (User.find_by_username(params[:id]))
      @username = params[:id]

    else 
      # redirect to 404 (root for now)
      redirect_to root_path, :notice=> "User not found!" 
    end
    @user = User.find_by_username(params[:username])

     #@user = current_user
    @username= User.find_by_username(params[:username])
    @currentuser = current_user
     @allcampaign = Createcampaign.all
      @toFollow = User.all.last(5)
  end


  def index
  end



  def new
  @user = User.new

end


 def following
    @title = "Following"
    @user = User.find(params[:id])
    @users = @user.followed_users.paginate(page: params[:page])
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
  end


end 

_follow_form


  <div id="follow_form">
  <% if current_user.following?(@user) %>
    <%= render 'unfollow' %>
  <% else %>
    <%= render 'follow' %>
  <% end %>
  </div>

when i click on follow: i get the following

undefined method `id' for nil:NilClass

Extracted source (around line #43):
41
42
43
44
45
46

 # follow another user
     def follow(other)
       active_relationships.create(followed_id: other.id)
     end

     # unfollow a user


             Another class is not being displayed when i render it in the page

             <% if current_user.username != @user.username %>
where is it

            <% if !current_user.following?(@user) %>
                <%= form_for(current_user.active_relationships.build) do |f| %>
                    <div><%= hidden_field_tag :followed_id, user.id %></div>
                    <%= f.submit "Follow", class: "btn btn-primary" %>
                <% end %>
            <% else %>
                <%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
                    html: { method: :delete }) do |f| %>
                    <%= f.submit "Unfollow", class: "btn" %>
                <% end %>
            <% end %>

<% end %>

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 76,990+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.