Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 932
max Avatar asked Sep 10 '16 11:09

max


1 Answers

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 
like image 125
Frederick Cheung Avatar answered Sep 24 '22 07:09

Frederick Cheung