【问题标题】:How to edit create registrations_controller action using devise?如何使用设计编辑创建 registrations_controller 操作?
【发布时间】:2020-08-21 01:43:38
【问题描述】:

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

我已成功按照说明 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

但我不知道该怎么做? (我是否只需添加代码并将“用户”替换为“资源”?)

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    您可以通过添加这样的块代码而不是复制设计代码来做到这一点

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多