我有一个用户模型parent
和子模型patient
。我想通过设计(用户)注册表单在患者模型中添加患者相关属性,但数据不会保存在患者模型中。
class RegistrationsController < Devise::RegistrationsController
def new
build_resource({})
resource.build_patient
respond_with self.resource
end
def create
super
end
end
private
def sign_up_params
params.require(resource_name).permit(:email, [patient_attributes:[:user_id, :phone, :address,:age]], :password, :password_confirmation)
end
这是我的用户和患者模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :patient
accepts_nested_attributes_for :patient
end
#####
class Patient < ActiveRecord::Base
belongs_to :user
end
这是嵌套表格:
<div class="field">
<%= f.fields_for :patient do |p| %>
phone <%= p.text_field :phone %>
address <%= p.text_field :address %>
age <%= p.text_field :age %>
<%end%>
</div>
当我填写表单并单击“提交”按钮时,这些是参数:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"b+5GjScdG1gSnPL1eRDMW9U6tWiL1+liJMHvBCWYO2DEqRPJIBKzpXE3HGHlDgJPVcB+ro3ZVi+fHmNCdri1Zw==", "user"=>{"username"=>"q", "email"=>"kh1@gmail.com", "patient_attributes"=>{"phone"=>"444444", "address"=>"lllllllll", "age"=>"55"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "user_type"=>"1"}, "commit"=>"Sign up"}
**Unpermitted parameter: patient_attributes**
答案 0 :(得分:0)
所以答案很简单 我在应用程序控制器中添加参数是这样的
devise_parameter_sanitizer.permit(:sign_up) | U | u.permit(:email,:password,:password_confirmation,:username,:user_type,patient_attributes:[:user_id,:phone,:address,:age]) 端强>
并稍微改变一下
<div class="field">
<%= f.fields_for :patient, Patient.new do |p| %>
</br>
phone <%= p.text_field :phone %>
</br>
address <%= p.text_field :address %>
</br>
age <%= p.text_field :age %>
<%end%>
</div>
答案 1 :(得分:0)
这可能会对您有所帮助。
只需从[patient_attributes:[:user_id, :phone, :address,:age]]
并使用
patient_attributes:[:user_id, :phone, :address,:age]
只。这应该对你有帮助。