从另一个模型注册用户 [英] Registering the user from another model
本文介绍了从另一个模型注册用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每个用户
只能有一个 MedicalHistory
让我有
class User < ActiveRecord::Base
acts_as_authentic
end
class MedicalHistory < ActiveRecord::Base
belongs_to :user
end
使用上面我可以成功地创建从 MedicalHistory
模式,在控制台的用户:
Using the above I can successfully create the user from the MedicalHistory
model in the console:
newuser = Medicalhistory.new
newuser.user = User.new
newuser.user.username='testusername'
newuser.user.password='blahblah'
newuser.user.password_confirmation='blahblah'
newuser.user.save
MedicalHistory侧面看起来像下面的
MedicalHistory Side looks like following
#controller
def new
@medicalhistory = MedicalHistory.new
@medicalhistory.user = User.new
end
#form
<%=...%>
<%=f.intput :cell_phone%>
<%=f.fields_for :user do |builder|%>
<%= render "registration_form", :f => builder %>
<% end %>
#Partial
<%=f.input :email%>
<%=f.input :password%>
<%=f.input :password_confirmation%>
错误
当提交表单我得到以下错误:
When submitting the form I am getting below error:
User(#-) expected, got ActiveSupport::HashWithIndifferentAccess(#)
错误是以下行:
Error is on following line:
#controller
def create
@medicalhistory = Medicalhistory.new(session[:medicalhistory_params]) #gives error
#somewhere here I should extract registration fields and register the user
end
问题
有没有一种方法可以让我避免部分(注册字段)领域进入了会话[:medicalhistory_params]
...有一个除了
还是什么?
Question
Is there a way I can avoid fields in the partial (registration fields) getting into the session[:medicalhistory_params]
...is there a except
or something?
推荐答案
更改模型如下:
class MedicalHistory < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
参考这里:的http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
这篇关于从另一个模型注册用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文