如何使用设计编辑创建 registrations_controller 操作?

How to edit create registrations_controller action using devise?

我正在制作一个具有传统用户模型和患者模型的简单应用。我希望用户在注册时自动成为患者。

我已设法按照说明进行操作 here,并且可以看到文件 users/registations_controller.rb,但我不确定要向其中添加什么。

我已经粘贴了来自 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

我也想添加功能来执行此操作:

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

但我不知道该怎么做? (我只是添加代码并将 'user' 替换为 'resource' 吗?)

您可以像这样添加块代码而不是复制 Devise 代码来做到这一点

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