国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

ruby - 怎麼在model裡邊獲取當(dāng)前用戶的ID等信息
黃舟
黃舟 2017-04-22 08:56:27
0
1
982

想要做一個(gè)操作日誌記錄執(zhí)行過的操作,
然後想在model裏邊獲取當(dāng)前操作用戶的ID,進(jìn)行操作的數(shù)據(jù)表名稱,還有操作類型
現(xiàn)在寫了一個(gè)moduel。

module RecordOperation
  extend ActiveSupport::Concern
  included do
    after_update :record_operation
    before_destroy :record_operation
    after_create :record_operation
    def record_operation

    end
end
黃舟
黃舟

人生最曼妙的風(fēng)景,竟是內(nèi)心的淡定與從容!

全部回覆(1)
巴扎黑

devise 文件裡是這麼寫的
To verify if a user is signed in, use the following helper:

user_signed_in?

For the current signed-in user, this helper is available:

current_user

You can access the session for this scope:

user_session

================================
至於在model中,一種直接點(diǎn)的方式是直接在方法中傳入user的實(shí)例或是id

redmine的處理方法我比較喜歡
User.rb

def self.current=(user)
    Thread.current[:current_user] = user
end

def self.current
  Thread.current[:current_user] ||= User.anonymous
end

然後在登入方法中

User.current = find_current_user
if user && user.is_a?(User)
  User.current = user
  start_user_session(user)
else
  User.current = User.anonymous
end

最終在任何地方都可以呼叫User.current來取得目前登入者的實(shí)例

devise其實(shí)我沒有深入用過,不太清楚他具體實(shí)現(xiàn)。
剛好好奇心起,晚上再回去試試

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板