问题描述
我查看了 Devise 代码并注意到大多数控制器产生正在创建的资源.
I was looking through the Devise code and noticed that most of the controllers yield the resource being created.
class Devise::RegistrationsController < DeviseController
# ...
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
# ...
这一定是某种可扩展性功能,但我真的不明白您如何将块传递给控制器操作?
This must be some sort of extendability feature but I don't really get how you would pass a block to to the controller action?
注意:这个问题是关于你将如何在 Rails 请求周期中实际执行,而不是关于 Ruby 中的块如何工作.
Note: This question is about how you would actually do it in the Rails request cycle, not about how blocks in Ruby work.
推荐答案
它是为了让子类可以重用 devise 提供的 create
实现,但能够挂钩到进程中.
It's to allow subclasses to reuse the create
implementation provided by devise, but being able to hook into the process.
例如你可能有类似的东西
For example you might have something like
class MyRegistrations < Devise::RegistrationsController
def create
super { |resource| ... }
end
end
这篇关于Devise 控制器中的“产量资源"有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!