I am creating form which accepts nested attributes: #doctor.rb
#user.rb
has_one :doctor
accepts_nested_attributes_for :doctor, reject_if: proc { |attributes| attributes['cartridge_name'].blank? }, allow_destroy: true
my strong parameters:
def user_params
params.require(:user).permit(:name,:birthday, :address, :phone,:email, :role, :social => [],
:doctor => [:position,:department_id,:salary_type,:date_came,:monthly_salary,:percentage_salary,:id],
end
But i got error:
Doctor(#70193541488440) expected, got ActionController::Parameters(#70193536484960)
Probably it is the problem with associations or with my strong params.
EDIT:
<%= form_for(@user, url:create_doctor_users_path) do |f| %>
<%= f.fields_for @doctor do |u| %>
<%= render 'doctor_attributes', f:u %>
<% end %>
<% end %>
EDIT2:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"birXhqxdplQMfZF5G8jDM9wi1nsBwkJfWsHcE8ZbPCxxemgR8ObmCFzODqoHAcTDo9jNMN0G0HK69A4hGy0Ddg==",
"user"=>{"name"=>"", "birthday"=>"", "address"=>"", "phone"=>"", "email"=>"",
"doctor"=>{"position"=>"", "department_id"=>"", "salary_type"=>"Зарплата", "date_came"=>"", "monthly_salary"=>"", "percentage_salary"=>""},
"educations_attributes"=>{"0"=>{"from"=>"", "to"=>"", "institution"=>"", "major"=>"", "_destroy"=>"false"}},
"job_experiences_attributes"=>{"0"=>{"from"=>"", "to"=>"", "institution"=>"", "position"=>"", "_destroy"=>"false"}}}, "commit"=>"Зарегестрировать"}
And also I think this is important, i am doing creation in controller this way:
@user = User.new
@doctor = Doctor.new
@user.job_experiences.build
@user.educations.build
I guess i should do:
@user.doctor.build
as with job_experiences, but i get error:
undefined method `build' for nil:NilClass
Fix your user_params
to this:
def user_params
params.require(:user).permit(:name,:birthday, :address, :phone,:email, :role, :social => [],
:doctor_attributes => [:position,:department_id,:salary_type,:date_came,:monthly_salary,:percentage_salary,:id]
^^^^^^^^^^^^^^^^^
end
Explain: If you are using accepts_nested_attributes_for
in the model, you should pass the permitted
params as doctor_attributes
not doctor
, under the hood rails create an object Doctor
as it described in your errors.
Read a wonderful article about accepts_nested_attributes_for
.
Fix your form to:
<%= f.fields_for :doctor, @user.build_doctor do |u| %>
<%= render 'doctor_attributes', f:u %>
<% end %>
when in your parameters you should see:
"doctor_attributes"=>{" ....}
instead of:
"doctor"=>{" ....}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.