加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何访问Padrino模型中的current_account?

发布时间:2020-12-16 21:01:18 所属栏目:百科 来源:网络整理
导读:我正在开发一个具有主应用程序和管理员应用程序的Padrino应用程序.当用户登录会话时,我只需运行以下两行来登录. account = Account.authenticate(params[:email],params[:password])set_current_account(account) 在Admin中的任何控制器中使用调试器 current
我正在开发一个具有主应用程序和管理员应用程序的Padrino应用程序.当用户登录会话时,我只需运行以下两行来登录.
account = Account.authenticate(params[:email],params[:password])
set_current_account(account)

在Admin中的任何控制器中使用调试器

current_account
#<Account @id=1 @name="John" @surname="Davies" @email="john.davies@gmail.com" @crypted_password="3456789" @role="admin">

在任何型号的调试器

current_account
*** NameError Exception: undefined local variable or method `current_account' for #<Post @id=1 @question="Why is this not working?" @answer="I have no idea">

我能够访问current_account以找出在Admin应用程序中登录的用户,但是在主应用程序中无法访问此变量.

我想要做的是我为所有模型创建一个活动源或审计跟踪,因此当在任何模型中创建/更新/销毁记录时,会在模型活动中创建一条新记录.话虽这么说,我需要访问模型中的current_account变量.

我搜索了解决方案,并提出了一个建议:

在admin / app.rb中

enable  :sessions
set :session_id,"my-global-session"

在app / app.rb中

register Padrino::Admin::AccessControl
register Padrino::Admin::Helpers

enable :sessions
set :session_id,"my-global-session"

它对我不起作用.有没有办法在我的模型中访问current_account?

感谢您的任何指导或建议.

解决方法

我不确定你想做什么,但我解决了类似这样的问题:
class Account
  # ...
  class << self
    attr_accessor :current
  end
  # ...
end

class Admin < Padrino::Application
  #...
  before do
    Account.current = current_account
  end
  # ...
end

然后使用Account.current来访问它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读