我正在尝试允许用户通过关注者宝石来关注或取消关注用户。这是我的表演视图:
<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
单击“关注用户”时,用户会收到“成功关注用户”的通知,但是预期的操作应该是该链接然后显示“取消关注用户”,但是不会发生。
答案 0 :(得分:0)
acts_as_follower rubygems版本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