2017-06-15 33 views
0

在我的項目我使用acts_as_follower寶石,以實現用戶followable_type「ApplicationRecord」,而不是RoR中的應用「用戶」

所以在我的User類中的後續/未追蹤功能,我有

class User < ApplicationRecord 
    ... 
    acts_as_follower 
    acts_as_followable 
    ... 
end 

但在db表Follow,該follower_typeUser(這是正確的),而followable_typeApplicationRecord

我是,如果你非常感謝可以幫助我瞭解爲什麼,在此先感謝..

的一些信息:

follows_controller.rb:

class FollowsController < ApplicationController 

def create 
    @user = User.find(params[:id]) 
    if([email protected]) 
     current_user.follow(@user) 
     redirect_to users_show_path(:id => @user.id) 
     flash[:notice] = "You are now following #{@user.username}." 
    end 
end 

def destroy 
    @user = User.find(params[:id]) 
    if([email protected]) 
     current_user.stop_following(@user) 
     redirect_to users_show_path(:id => @user.id) 
     flash[:notice] = "You are no longer following #{@user.username}." 
    end 
end 
end 

schema.rb:

create_table "follows", force: :cascade do |t| 
    t.string "followable_type" 
    t.integer "followable_id",     null: false 
    t.string "follower_type" 
    t.integer "follower_id",      null: false 
    t.boolean "blocked",   default: false, null: false 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.index ["followable_id", "followable_type"], name: "fk_followables" 
    t.index ["follower_id", "follower_type"], name: "fk_follows" 
end 

follow.rb

class Follow < ActiveRecord::Base 

    extend ActsAsFollower::FollowerLib 
    extend ActsAsFollower::FollowScopes 

    # NOTE: Follows belong to the "followable" interface, and also to followers 
    belongs_to :followable, :polymorphic => true 
    belongs_to :follower, :polymorphic => true 

    def block! 
    self.update_attribute(:blocked, true) 
    end 

end 
+0

你如何創建'association'?分享代碼的控制器和型號'follow_controller.rb' –

+0

@ Md.FarhanMemon我已編輯的問題,共享代碼,謝謝 –

+0

ApplicationRecord(如在標題)或ActiveRecord(如在說明中)? – MikDiet

回答

0

因爲我請參閱gem description,只有主分支支持Rails 5,可能是這個原因。

所以你需要在你的Gemfile,更換

gem "acts_as_follower" 

通過

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

好吧,我已經按照你的建議添加了寶石,但問題是一樣的.... –

相關問題