在Ruby on Rails的Restful身份验证中,#self.included(base)是什么?(What does #self.included(base) do in Ruby on Ra

系统教程 行业动态 更新时间:2024-06-14 16:57:18
在Ruby on Rails的Restful身份验证中,#self.included(base)是什么?(What does #self.included(base) do in Ruby on Rails' Restful Authentication?)

我以为我们会做

helper_method :current_user, :logged_in?, :authorized?

使这些控制器方法可用作视图中的辅助方法。 但是在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被调用在任何地方。

I thought we would do

helper_method :current_user, :logged_in?, :authorized?

to make these controller methods available for use as helper methods in views. But in Restful Authentication's lib/authenticated_system.rb, I see:

# 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

Why is it done this way instead of that single line? Also, I don't see included being called anywhere.

最满意答案

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

更多信息: 一个ruby mixin教程 。

The self.included function is called when the module is included. It allows methods to be executed in the context of the base (where the module is included).

More info: a ruby mixin tutorial.

更多推荐

本文发布于:2023-04-12 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bf896e20c5bf724ad4538cd1ebe7046b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Restful   Rails   Ruby   base

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!