本文介绍了如何使用dev编辑create registrations_controller操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的应用程序,该应用程序具有传统的用户模型,也具有患者模型.我希望用户在注册时自动成为患者.

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.

我设法按照此处的说明进行操作,并且可以看到文件 users/registations_controller.rb ,但我不确定要添加什么.

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.

我从粘贴了现有的devise创建代码.这里

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

推荐答案

您可以通过添加这样的块代码而不是复制Devise代码来实现

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

这篇关于如何使用dev编辑create registrations_controller操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:07