tommyd456
tommyd456

Reputation: 10673

Overriding Devise Controller

Experimenting with Devise 3.0 for Rails 4 and thinking about integrating into an existing app to replace my own Authentication system.

My own users_controller communicated with an API (Stripe) during registration and I'm not sure how I would include this functionality if I were to use Devise?

Am I suppose to override/extend the Devise Controller in some way? Any help would be much appreciated.

Upvotes: 0

Views: 423

Answers (2)

Semjon
Semjon

Reputation: 1023

For future googlers: it is possible to call super in an action of inherited controller with block and do whatever you want, without overwriting Devise code:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    super do |user|
      NotificationMailer.user_created(user).deliver
    end
  end
end

Upvotes: 3

Charizard_
Charizard_

Reputation: 1245

You can define the actions in the your controller and call super inside it, which will include the functionality of the corresponding devise action. And you can also add your own functionality into it.

Eg: Consider the Registrations controller (Registrations Controller)

Here, you can use the devise's create action code of Registration Controller in you own create action of Registration Controller and add your functionality as commented in the code.

class RegistrationsController < Devise::RegistrationsController
  .
  .
  .

  def create
    build_resource(sign_up_params)

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        # Here the user successfully signs up, so you can use your stripe API calls here.
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end

  end

end 

Upvotes: 1

Related Questions