Search code examples
ruby-on-railsdevise

How to edit create registrations_controller action using devise?


I'm making a simple app that has the traditional User model, but also a Patients model. I want a user to automatically become a patient on sign up.

I've managed to follow instructions here, and can see the file users/registations_controller.rb, but I'm not sure what to add to it.

I have pasted the existing devise create code from here

  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end

and I want to add functionality to do this too:

    # @user = User.new(user_params)
    @patient = @user.build_patient
    @patient.save

But I don't know how to do that? (do I just add the code and replace 'user' with 'resource'?)


Solution

  • You can do it by adding block code like this instead of copy the Devise code

    class YourController < Devise::RegistrationsController
      def create
        super do |user|
          @patient = user.build_patient
          @patient.save
        end
      end
    end