取消关注用户不工作:作为关注者 gem

unfollow user not woking: acts as follower gem

我试图允许用户通过作为关注者的行为来关注或取消关注用户 gem。这是我的展示视图:

     <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>

我的路线:

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

结束

和我的控制器:

   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

最后是我的模型:

   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

单击 "Follow user" 时,用户会收到通知 "successfully follow user" 但预期的操作应该是 link 然后会说 "unfollow user" 然而,这不会发生。

acts_as_follower ruby​​gems 版本 0.2.1 已过时,从 2013 年开始。您需要更改您的 gemfile 行:

gem 'acts_as_follower'

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

或者你可以在这里接受我的 PR:https://github.com/westche/sample_app/pull/1