Devise控制器的“产出资源”是什么? [英] What does 'yield resource' in the Devise controllers do?

查看:95
本文介绍了Devise控制器的“产出资源”是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看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 in the Rails request cycle, not about how blocks in Ruby work.

推荐答案

它允许子类重用 create 由devise提供的实现,但能够挂钩进程。

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控制器的“产出资源”是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆