gpt4 book ai didi

ruby-on-rails - #self.included(base) 在 Ruby on Rails 的 Restful 身份验证中起什么作用?

转载 作者:行者123 更新时间:2023-12-03 06:01:17 25 4
gpt4 key购买 nike

我以为我们会这么做

helper_method :current_user, :logged_in?, :authorized?

使这些 Controller 方法可用作 View 中的辅助方法。但在 Restful Authentication 的 lib/authenticated_system.rb 中,我看到:

# Inclusion hook to make #current_user and #logged_in?
# available as ActionView helper methods.
def self.included(base)
base.send :helper_method, :current_user, :logged_in?, :authorized? if base.respond_to? :helper_method
end

为什么要这样做而不是单行?另外,我没有看到 included 在任何地方被调用。

最佳答案

包含模块时会调用 self.included 函数。它允许在基础上下文(包含模块的地方)中执行方法。

更多信息:a ruby mixin tutorial

关于ruby-on-rails - #self.included(base) 在 Ruby on Rails 的 Restful 身份验证中起什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5160780/

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