acts_as_follower 以下用户的问题

acts_as_follower issue with following users

我在设计中创建了一个用户模型:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_one :profile, dependent: :destroy
  after_create :build_profile

  acts_as_follower

end

其中有一个关联的 profileuser。我正在使用 acts_as_follower gem 让这个模型能够跟随另一个模型。

我希望 User 跟随 Profile,所以我的配置文件模型设置如下:

class Profile < ApplicationRecord
  belongs_to :user

  has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "https://s-media-cache-ak0.pinimg.com/564x/f7/61/b3/f761b3ae57801975e0a605e805626279.jpg"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/

  acts_as_followable

end

根据 gem,Profile 模型应该是可遵循的,我就是这样设置的。

我 运行 根据需要进行迁移 rails generate acts_as_follower

在我的profile_controller iv中设置了以下方法:

  def follow
    @profile = Profile.find(params[:id])
    current_user.follow(@profile)
    redirect_to :back
  end

  def unfollow
    @profile = Profile.find(params[:id]) 
    current_user.stop_following(@profile)
    redirect_to :back
  end

在我的 route.rb 文件中有以下内容:

  resources :profiles do
    member do
      get :follow
      get :unfollow
    end
  end

现在在我的个人资料显示视图 iv 中按如下方式设置按钮:

  <% if !@profile.followed_by?(current_user) %>
    <%= link_to "Follow", follow_profile_path(@profile), class: "btn btn-success btn-outline btn-sm"%>
  <% end %>

根据我的知识和文档,这应该有效,但由于某种原因,它没有关注用户或在 userprofile 之间创建关联。

我想出来了,在我从 master 分支安装的 gemfile 中,所以将你的 gemfile 更改为:

gem "acts_as_follower", github: "tcocca/acts_as_follower"