1

Im adding acts as follower to my app. but its coming up with the below error regarding my follow method when i click on the follow button which is on a users show page. im not sure what it means or how to resolve. ive looked at other questions and read docs but no luck. im not even 100% sure if my method is correct. thanks.

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
         has_many :tweets
         mount_uploader :photo, PhotoUploader
    validates :photo, presence: :true
    acts_as_followable
    acts_as_follower


end

Started POST "/users/3/follow" for 127.0.0.1 at 2018-10-22 18:58:33 +0100
Processing by UsersController#follow as HTML
  Parameters: {"authenticity_token"=>"qsRjm2CxBJ5jy8zAyL8m6hJAvOS4kZuQwwsLLcD2NmbgCs+uDsOoOtceQKJ69CXMVskIOSPAk0/R6t1yroOM7g==", "id"=>"3"}
  User Load (2.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 7], ["LIMIT", 1]]
  ↳ /Users/benherring/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:12
  Follow Load (0.8ms)  SELECT  "follows".* FROM "follows" WHERE "follows"."follower_id" = $1 AND "follows"."follower_type" = $2 AND "follows"."followable_id" = $3 AND "follows"."followable_type" = $4 LIMIT $5  [["follower_id", 7], ["follower_type", "User"], ["followable_id", 3], ["followable_type", "ApplicationRecord"], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:13
   (0.3ms)  BEGIN
  ↳ app/controllers/users_controller.rb:13
   (0.2ms)  ROLLBACK
  ↳ app/controllers/users_controller.rb:13
Completed 500 Internal Server Error in 16ms (ActiveRecord: 4.0ms)


  
TypeError (no implicit conversion of nil into String):
  
app/controllers/users_controller.rb:13:in `follow'

enter image description here

class UsersController < ApplicationController


  def show

    @user = User.find(params[:id])
    @tweets = @user.tweets.order('created_at DESC')
    authorize @user
  end

  def follow
     @user = User.find(params[:id])
     current_user.follow(@user)
    authorize @user
    @follow = Follow.find_by(follower: @current_user, followable: @user)
  end

  def unfollow
    @user = User.find(params[:id])
    current_user.stop_following(@user)
  end
end

<% if current_user.following?(@user) %>
  <%= button_to "Following", {action: "unfollow", id: @user.id}, method: "post", class: "btn btn-secondary btn_unfollow"%>
<% else  %>
  <%= button_to "Follow", {action: "follow", id: @user.id}, method: "post", class: "btn btn-primary btn_follow" %>
<% end %>

]2]2

b.herring
  • 563
  • 2
  • 18
  • Can you post the server log when generated when you click on Follow button – Pavan Oct 22 '18 at 17:46
  • I suspect you aren't sending your params correctly. I would try doing in your button_to `params: { action: ....}`. Do me a favor and tell me what happens when you try doing either a byebug and seeing what params[:id] returns before line 13 in your follow or just printing it out and telling me what that returns – Gabriel Oct 22 '18 at 17:47
  • Replace `current_user.follow(@user)` after `authorize @user` – Pavan Oct 22 '18 at 18:04
  • thanks, however ive changed this but i still have the same issue and same error. ive also added the server log. – b.herring Oct 22 '18 at 18:08
  • What is the value of `current_user`? Make sure its not nil. – Pavan Oct 22 '18 at 18:09
  • Hmm that is interesting. I would checkout what current_user is like Pavan is asking and then I would also consider stepping into the current_user.follow and see what is going on there – Gabriel Oct 22 '18 at 18:16
  • current_user has all the info of the current user, so that seems ok. im not 100% sure what you mean by step into current_user.follow, but i typed it into the console at the bottom of the error page and this came up again "TypeError: no implicit conversion of nil into String" – b.herring Oct 22 '18 at 18:50
  • When I say step i mean when you do byebug and you are on line 13 step into that method. Make sure that @user is a user object and it sounds like current_user is ok. When you step into the current_user.follow it will go into that method. Start looking around within that method and see if everything is ok. If you're getting that TypeError message when you try stepping into that follow method then I'm not entirely sure what the issue is. Maybe something with the gem. – Gabriel Oct 22 '18 at 18:59
  • Have you checked that `current_user` is not nil? Usually you would have a `before_action :authenticate_user!` callback that authenticates the user. This depends on what authentication solution you are using. – max Oct 22 '18 at 19:07
  • It might be related to how you set up the gem.. Could you include your User class definition? – ldeld Oct 23 '18 at 13:47
  • @b.herring did you ever figure out what was the issue? I'm genuinely curious. – Gabriel Oct 24 '18 at 22:55
  • @Gabriel not i didn't unfortunatly. but thank you for your help in trying to solve. – b.herring Oct 31 '18 at 01:35

0 Answers0