【发布时间】:2017-01-05 06:47:53
【问题描述】:
我在设计中创建了一个用户模型:
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
与user 关联的profile。我正在使用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
Profile 模型应该根据 gem 是可遵循的,这就是我设置它的方式。
我根据需要运行了迁移rails generate acts_as_follower
在我的profile_controlleriv 中设置如下方法:
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
现在在我的个人资料显示视图中设置按钮如下:
<% if !@profile.followed_by?(current_user) %>
<%= link_to "Follow", follow_profile_path(@profile), class: "btn btn-success btn-outline btn-sm"%>
<% end %>
根据我的知识和文档,这应该可以工作,但由于某种原因,它不会跟随用户或创建 user 和 profile 之间的关联以跟随。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 acts-as-follower