ruby-on-rails - Rails ActiveSupport::关注点和方法评估

标签 ruby-on-rails ruby devise activesupport-concern

我有这个应用程序,它使用 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 falsecurrent_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 >

这在底层是如何工作的?

最佳答案

  1. current_user 是 Devise 提供的辅助方法,而不是局部变量。

  2. 没有名为 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/

相关文章:

javascript - 如何将变量从 Controller 传递到 application.js?

ruby-on-rails - Ruby on Rails 将通配符子域路由到 Controller /操作

ruby-on-rails - 在 1 个操作中选择和拒绝 2 个数组的 ruby​​ 数组(可枚举)方法

ruby-on-rails - 如何在 Rails4 测试中使用 Devise 登录/注销用户

ruby-on-rails - 注册后欢迎快讯?

ruby-on-rails-3 - 如何使用phonegap和devise完成登录(Rails)

ruby-on-rails - 预编译 Rails 5 以包含所有 Assets

ruby-on-rails - 如何解决传递货币格式字符串时 erb 抛出的错误

mysql - 使用 lampp 安装 gem mysql2

mysql - Ruby 和 mysql2-迭代结果集中的行子集