Ruby on Rails - 从模型访问控制器变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2419120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails - Access controller variable from model
提问by RailsSon
I am trying to access an instance variable which is set in the controller in the model. The controller is the products controller and the model is the products model. The instance variable is a instance of another model called account.
我正在尝试访问在模型中的控制器中设置的实例变量。控制器是产品控制器,模型是产品模型。实例变量是另一个名为 account 的模型的实例。
The instance variable is @current_account
实例变量是 @current_account
When I run the code nothing happens, I do not get an error. Does anyone know where I can find something read about access instance variables set in the controller from the model?
当我运行代码时,没有任何反应,我没有收到错误消息。有谁知道我在哪里可以找到有关模型中控制器中设置的访问实例变量的信息?
Thanks
谢谢
Eef
埃夫
回答by jeem
You shouldn't generally try to access the controller from the model for high-minded issues I won't go into.
您通常不应该尝试从模型访问控制器以解决我不会讨论的高尚问题。
I solved a similar problem like so:
我解决了类似的问题:
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
Then just access the current account with Account.current
然后只需访问当前帐户 Account.current
回答by ibaixas
DISCLAIMER: The following code breaks MVC conventions, that said...
免责声明:以下代码违反了 MVC 约定,即...
Using class attributes can probably lead to thread safety issues. I would use Thread.current + around_filter to store controller related data at thread level, and ensure it gets cleared just before the request finishes:
使用类属性可能会导致线程安全问题。我会使用 Thread.current + around_filter 在线程级别存储控制器相关数据,并确保它在请求完成之前被清除:
class ApplicationController < ActionController::Base
around_filter :wrap_with_hack
def wrap_with_hack
# We could do this (greener solution):
# http://coderrr.wordpress.com/2008/04/10/lets-stop-polluting-the-threadcurrent-hash/
# ... but for simplicity sake:
Thread.current[:controller] = self
begin
yield
ensure
# Prevent cross request access if thread is reused later
Thread.current[:controller] = nil
end
end
end
Now the current controller instance will be avaliable globaly during the request processing through Thread.current[:controller]
现在当前控制器实例将在通过 Thread.current[:controller] 请求处理期间全局可用
回答by raf
If you needto access a controller variable from a model it generally means your design is wrong because a controller serves as bridge between view and model (at least in Rails), controller gets info from models, models shouldn't know anything about controllers, but if you want to do it anyway you can do it just as jeem said, but I'd rather do:
如果您需要从模型访问控制器变量,这通常意味着您的设计是错误的,因为控制器充当视图和模型之间的桥梁(至少在 Rails 中),控制器从模型中获取信息,模型不应该知道关于控制器的任何信息,但是如果你无论如何都想这样做,你可以像jeem所说的那样做,但我宁愿这样做:
class << self
attr_accessor :current
end
instead of
代替
cattr_accessor :current
you can see why here => cattr_accessor doesn't work as it should
你可以在这里看到为什么 => cattr_accessor 不能正常工作
回答by Jacob
I can't comment directly so I'll post here: the accepted answer does not seem to be right. As @vise notes, class variables are shared across requests. So unless there's just one current account for the entire app, this won't behave as expected.
我不能直接发表评论,所以我会在这里发帖:接受的答案似乎不正确。正如@vise 所指出的,类变量在请求之间共享。因此,除非整个应用程序只有一个当前帐户,否则这不会按预期运行。
For more, see the accepted answer by @molf here: Is Rails shared-nothing or can separate requests access the same runtime variables?
有关更多信息,请参阅 @molf 此处接受的答案:Rails 是无共享的还是可以单独的请求访问相同的运行时变量?
回答by ghoppe
I'm not sure if I understand the question exactly, but I'll take a stab.
我不确定我是否完全理解这个问题,但我会尝试一下。
I think if you need to access a controller instance variable from the model then you either need to make it an attribute in the model, or move your logic to the other class controller, not model.
我认为如果您需要从模型访问控制器实例变量,那么您要么需要将其作为模型中的一个属性,要么将您的逻辑移动到另一个类控制器,而不是模型。

