gpt4 book ai didi

ruby-on-rails - Ruby on Rails - 从模型访问 Controller 变量

转载 作者:数据小太阳 更新时间:2023-10-29 06:31:19 24 4
gpt4 key购买 nike

我正在尝试访问在模型的 Controller 中设置的实例变量。 Controller 是产品 Controller ,模型是产品模型。实例变量是另一个名为 account 的模型的实例。

实例变量是@current_account

当我运行代码时没有任何反应,我没有收到错误。有谁知道我在哪里可以找到有关从模型访问 Controller 中设置的实例变量的信息?

谢谢

头皮

最佳答案

对于我不会讨论的高尚问题,您通常不应该尝试从模型访问 Controller 。

我解决了类似的问题:

class Account < ActiveRecord::Base
cattr_accessor :current
end

class ApplicationController < ActionController::Base
before_filter :set_current_account
def set_current_account
# set @current_account from session data here
Account.current = @current_account
end
end

然后只需使用 Account.current 访问当前帐户

关于ruby-on-rails - Ruby on Rails - 从模型访问 Controller 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2419120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com