第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

訪問模型中的current_user

訪問模型中的current_user

慕村9548890 2019-08-19 10:17:43
訪問模型中的current_user我有3張桌子items (columns are:  name , type)history(columns are: date, username, item_id)user(username, password)當(dāng)用戶說“ABC”登錄并創(chuàng)建新項(xiàng)目時(shí),將使用以下after_create過濾器創(chuàng)建歷史記錄。如何通過此過濾器將此用戶名“ABC”分配給歷史記錄表中的用戶名字段。class Item < ActiveRecord::Base   has_many :histories   after_create :update_history  def update_history     histories.create(:date=>Time.now, username=> ?)    endend我在session_controller中的登錄方法def login  if request.post?     user=User.authenticate(params[:username])     if user       session[:user_id] =user.id       redirect_to( :action=>'home')       flash[:message] = "Successfully logged in "     else       flash[:notice] = "Incorrect user/password combination"       redirect_to(:action=>"login")     end   endend我沒有使用任何身份驗(yàn)證插件。如果有人能告訴我如何在不使用插件(如userstamp等)的情況下實(shí)現(xiàn)這一點(diǎn),我將不勝感激。
查看完整描述

3 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

Rails 5

聲明一個(gè)模塊

module Current
  thread_mattr_accessor :userend

分配當(dāng)前用戶

class ApplicationController < ActionController::Base
  around_action :set_current_user  def set_current_user    Current.user = current_user    yield
  ensure
    # to address the thread variable leak issues in Puma/Thin webserver
    Current.user = nil
  end             end

現(xiàn)在您可以將當(dāng)前用戶稱為 Current.user

有關(guān)thread_mattr_accessor的文檔

Rails 3,4

訪問current_user模型內(nèi)部并不常見。話雖如此,這是一個(gè)解決方案:

class User < ActiveRecord::Base
  def self.current    Thread.current[:current_user]
  end

  def self.current=(usr)
    Thread.current[:current_user] = usr  endend

current_user在a around_filter中設(shè)置屬性ApplicationController

class ApplicationController < ActionController::Base
  around_filter :set_current_user  def set_current_user    User.current = User.find_by_id(session[:user_id])
    yield
  ensure
    # to address the thread variable leak issues in Puma/Thin webserver
    User.current = nil
  end             end

設(shè)置current_user成功后的身份驗(yàn)證:

def login  if User.current=User.authenticate(params[:username], params[:password])
    session[:user_id] = User.current.id
    flash[:message] = "Successfully logged in "
    redirect_to( :action=>'home')
  else
    flash[:notice] = "Incorrect user/password combination"
    redirect_to(:action=>"login")
  endend

最后,指的是current_userupdate_historyItem。

class Item < ActiveRecord::Base
  has_many :histories
  after_create :update_history  def update_history
    histories.create(:date=>Time.now, :username=> User.current.username) 
  endend


查看完整回答
反對(duì) 回復(fù) 2019-08-19
?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

如果用戶創(chuàng)建了一個(gè)項(xiàng)目,該項(xiàng)目是否應(yīng)該有一個(gè)belongs_to :user子句?這樣你就after_update可以做到

History.create :username => self.user.username


查看完整回答
反對(duì) 回復(fù) 2019-08-19
  • 3 回答
  • 0 關(guān)注
  • 719 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)