0

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.stack.imgur.com/Lnirg.png

westman2222
  • 663
  • 1
  • 12
  • 30
  • What happens when you refresh the page? Does it change to "unfollow user"? – RudyOnRails Feb 16 '19 at 02:35
  • it does not, the link still says "follow user"....However, the notification / notice / alert does say "successfully followed user" – westman2222 Feb 16 '19 at 02:35
  • Can you add a snippet of the follow POST request in your server log? I don't think you can make a POST with a `link_to` unless you are doing some sort of javascript with it. – RudyOnRails Feb 16 '19 at 02:38
  • I am learning rails and therefore do not how to view my server log..... apologies I am a newbie – westman2222 Feb 16 '19 at 02:41
  • No problem. How do you start your rails server? Do you use the terminal? Is there a window showing a bunch of text flying by? – RudyOnRails Feb 16 '19 at 02:44
  • yes! I didn't realize that this is what it was called, I pasted a screen snip – westman2222 Feb 16 '19 at 02:46
  • Ok great. Clear that screen (CmndK, then click on the button to follow one person, then go and copy and paste the request log in your post. – RudyOnRails Feb 16 '19 at 02:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188503/discussion-between-westman2222-and-rudyonrails). – westman2222 Feb 16 '19 at 02:50

1 Answers1

0

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

RudyOnRails
  • 1,819
  • 3
  • 17
  • 26