Search code examples
ruby-on-railsruby-on-rails-4acts-as-follower

unfollow user not woking: acts as follower gem


I am attempting to allow a user to follow or unfollow a user via the acts as follower gem. This is my show view:

     <tbody>
                      <% @runs.each do |run| %>
                          <td><%= run.user.try(:username) %><p> 
                            <% if current_user.following?(@user) %> 
                            <%= link_to "Unfollow", unfollow_user_path(@user.username), method: :delete %></p>
                            <% else %>  
                            <%= link_to "Follow", follow_user_path(@user.username), method: :post %> </td> 
                            <% end %>
                          <td><%= run.Run_Type %></td>
                          <td><%= run.Location %></td>
                          <td><%= run.Start_Time %></td>
                          <td><%= run.Pace %></td>
                          <td><%= run.Miles %></td>
                          <td><%= run.Run_Date %></td>
                          <td><%= link_to 'Show', run %></td>
                          <% if run.user == current_user %>
                          <td><%= link_to 'Edit', edit_run_path(run) %></td> 
                          <td><%= link_to 'Destroy', run, method: :delete, data: { confirm: 'Are you sure?' } %></td>
                           <% end %>
                        </tr>
                      <% end %>
                    </tbody>
                  </table>

and my routes:

  resources :users, only: :show, param: :username do
member do
  post 'follow', to: 'follows#create'
  delete 'unfollow', to: 'follows#destroy'
end

end

and my controller:

   class FollowsController < ApplicationController
  before_action :authenticate_user!

  def create
    user = User.find_by(username: params[:username])
    Follow.create(followable: user, follower: current_user)
    redirect_to user_path(user.username), notice: "Successfully followed user"
  end

  def destroy
    user = User.find_by(username: params[:username])
    Follow.find_by(followable: user, follower: current_user).destroy
    redirect_to user_path(user.username), notice: "Successfully unfollowed user"
  end
end

finally my model:

   class Follow < ApplicationRecord

  extend ActsAsFollower::FollowerLib
  extend ActsAsFollower::FollowScopes

  # NOTE: Follows belong to the "followable" and "follower" interface
  belongs_to :followable, :polymorphic => true
  belongs_to :follower,   :polymorphic => true

  def block!
    self.update_attribute(:blocked, true)
  end

end

When "Follow user" is clicked, the user gets the notification "successfully follow user" but the expected action should be that that link then would say "unfollow user" however, this does not happen.

enter image description here

[1]: https://i.sstatic.net/Lnirg.png


Solution

  • The acts_as_follower rubygems version 0.2.1 is outdated, from 2013. You need to change your gemfile line from:

    gem 'acts_as_follower'
    

    to

    gem 'acts_as_follower', github: 'tcocca/acts_as_follower', branch: 'master'
    

    Or you can just accept my PR here: https://github.com/westche/sample_app/pull/1