0

im trying to add acts a follower to my app so a user can follow other users and on the tweets index page only display the current users tweets and the tweets of the people they are following. i currently have a "follow" button on the user show page, but i would expect it to change into "following" on a click, but nothing changes. ive looked at other questions and documentation, but no luck. thank you.

Users show view:

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

Users Controller:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @tweets = @user.tweets.order('created_at DESC')
    authorize @user
  end

  def follow
    @current_user.follow(@user)
    @follow = Follow.find_by(follower: @current_user, followable: @user)
     # @user = User.find(params[:id])
     # current_user.follow(@user)
     #  current_user.follow(@user)
     #  redirect to user_path(@user)
    # respond_to :js
  end

  def unfollow
    @user = User.find(params[:id])
    @current_user.stop_following(@user)
    # current_user.stop_following(@user)
    # redirect_to user_path(@user)
    # respond_to :js
  end
end

Routes:

resources :users do
  member do
    get :follow
    get :unfollow
  end
end
anothermh
  • 9,815
  • 3
  • 33
  • 52
b.herring
  • 563
  • 2
  • 18
  • Your route should also be `post :follow` and `post :unfollow`. You currently have them set as get requests. – Hamed Saadat Oct 21 '18 at 16:18
  • thanks, however it now comes up with "undefined method `follow' for nil:NilClass" when i click on the follow button. sorry im quite new to this @HamedSaadat – b.herring Oct 21 '18 at 16:29

2 Answers2

0

You're adding an ajax response by doing remote:true. You need to add a follow.js.erb and an unfollow.js.erb view that will re-render your partial.

Hamed Saadat
  • 429
  • 5
  • 8
  • ah thanks, i've removed it for now and add ajax later when it works. however when i click on the follow button now it comes up with "No route matches [POST] "/users/3/follow"". i changed get to post on follow in the routes, however now it comes up with "undefined method `follow' for nil:NilClass" in my follow method. – b.herring Oct 21 '18 at 16:18
0

Try and remove the remote: true part from your view:

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

Add post method for both follow and unfollow actions on routes.rb:

resources :users do
  member do
    post :follow
    post :unfollow
  end
end

and try current_user instead of @current_user in your UsersController if you're using Devise as a authentication solution:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @tweets = @user.tweets.order('created_at DESC')
    authorize @user
  end

  def follow
    current_user.follow(@user)
    @follow = Follow.find_by(follower: current_user, followable: @user)
     # @user = User.find(params[:id])
     # current_user.follow(@user)
     #  current_user.follow(@user)
     #  redirect to user_path(@user)
    # respond_to :js
  end

  def unfollow
    @user = User.find(params[:id])
    current_user.stop_following(@user)
    # current_user.stop_following(@user)
    # redirect_to user_path(@user)
    # respond_to :js
  end
end
John Baker
  • 2,315
  • 13
  • 12
  • thanks, ive made your changes, however it said "undefined method `id' for nil:NilClass" on the follow method. I then added @user = User.find(params[:id]) to the follow method, which i wasnt sure if that as correct, but it now comes up with 'no implicit conversion of nil into String' with current_user.follow(@user) in red. – b.herring Oct 21 '18 at 22:22