我有这个应用程序,它使用 Devise 和 current_user
帮助程序。当我创建一个模块时,在我提到它的归属后,current_user
变为 nil
,即使它从未发生过。
class PagesController < ApplicationController
include ModuleTest
def index
a_test_method
end
end
以及ModuleTest:
module ModuleTest
extend ActiveSupport::Concern
def a_test_method
puts "(BEFORE)===========> #{current_user.inspect}"
current_user = nil if false
puts "(AFTER) ===========> #{current_user.inspect}"
end
end
输出:
(BEFORE)===========> #<User id: 1>
(AFTER) ===========> nil
但是,如果我删除/注释掉这一行# current_user = nil if false
,current_user
仍然有效:
(BEFORE)===========> #<User id: 1>
(AFTER) ===========> #<User id: 1>
这与惰性评估有一定关系吗?
编辑
整个问题取决于在未评估语句时 Ruby 如何定义变量:
2.3.4 (main):0 > defined? this_never_seen_variable_before
=> nil
2.3.4 (main):0 > this_never_seen_variable_before = "value" if false
=> nil
2.3.4 (main):0 > defined? this_never_seen_variable_before
=> "local-variable"
2.3.4 (main):0 >
2.3.4 (main):0 > this_never_seen_variable_before_2
NameError: undefined local variable or method `this_never_seen_variable_before_2' for main:Object
from (pry):119:in `<main>'
2.3.4 (main):0 > this_never_seen_variable_before_2 = "value" if false
=> nil
2.3.4 (main):0 > this_never_seen_variable_before_2
=> nil
2.3.4 (main):0 >
这在底层是如何工作的?
最佳答案
current_user
是 Devise 提供的辅助方法,而不是局部变量。没有名为
current_user=
的此类辅助方法。您可以通过将current_user = nil
更改为self.current_user = nil
来证明这一点,并看到它崩溃。但这与您的问题无关。
结果是,您在 2 个 puts
之间定义了一个局部变量 current_user
,它隐藏了同名的辅助方法。
奇怪的是,虽然current_user = nil
因为if false
而没有被执行,但是局部变量仍然被定义,并且它的值被隐式设置为零
。这就是为什么您的第二个 puts
显示 nil
。即使您将 current_user = nil
更改为 current_user = :someone
,您的第二个 puts
仍应显示 nil
。
关于ruby-on-rails - Rails ActiveSupport::关注点和方法评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47587079/