How to edit create registrations_controller action using devise?

老子叫甜甜 提交于 2021-02-04 08:23:22

问题


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'?)


回答1:


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


来源:https://stackoverflow.com/questions/63515348/how-to-edit-create-registrations-controller-action-using-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!