问题描述
我正在制作一个简单的应用程序,它具有传统的用户模型和患者模型.我希望用户在注册时自动成为患者.
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.
我已经粘贴了来自 的现有设计创建代码这里
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'?)
推荐答案
你可以通过添加这样的块代码而不是复制设计代码来做到这一点
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
这篇关于如何使用设计编辑 create registrations_controller 动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!