Search code examples
ruby-on-railsdevise

What does 'yield resource' in the Devise controllers do?


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?

Note: This question is about how you would actually do it in the Rails request cycle, not about how blocks in Ruby work.


Solution

  • 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