我想我们会的

helper_method :current_user, :logged_in?, :authorized?

使这些控制器方法可用作视图中的辅助方法.但在Restful认证的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相关问答推荐

ruby on rails:错误未知关键字::当使用google-cloud-storage下载文件时,soft_deleted

仅当未在带有ENV[';Dyno';]的Heroku问题上运行时才初始化Sidekiq Cron

轨道强参数,怎么允许空参数?

Ruby on Rails - 外键不匹配

获取所有属于模型的嵌套对象

ruby on rails 中的自定义关联

您如何查看在 Rails 中触发回调的关联模型?

Rails - 如何使用子对象的嵌套属性和强参数填充父对象ID?

Rails - 如何从 http://example.com 重定向到 https://www.example.com

如果 URL 不存在,请将 http(s) 添加到 URL?

如何确定 Rails 是从 CLI、控制台还是作为服务器运行?

如何在 rspec 中运行单个测试?

是否有与 PHP 的 isset() 等效的 Rails?

使用 RSpec 2 关闭一个规范的事务性固定装置

Rails:如何在表单的必填字段上禁用星号?

通过重定向使闪存哈希保持不变

rbenv:没有 gemsets 生存

即使X-Frame-Options为ALLOWALL,也无法在 iframe 中显示我的 rails 4 应用程序

Rails 3:乘客找不到Bundle 程序安装的 git gem

Ruby/Rails CSV 解析,UTF-8 中的无效字节序列