我是rails的新手,目前正在尝试创建一款应用,但我似乎无法让它发挥作用。这是我在模型中的设置。
class User < ActiveRecord::Base
has_one :doctor, dependent: :destroy
accepts_nested_attributes :doctor
end
class Doctor < ActiveRecord::Base
belongs_to :user, :dependent => :destroy
end
在我的users_controller中,这是我的代码:
class UsersController < ApplicationController
def show
@user = current_user
# render text: @user.inspect
end
def new
@user = User.new
@user.build_doctor`
end
def create
# binding.pry
@user = User.new(user_params)
if @user.save
sign_in @user
redirect_to dashboard_path
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:role, :lastname, :firstname, :middlename, :email, :password, :password_confirmation, :doctor_attributes => [:institution, :license_number])
end
end
我的观点:
<ul id="cbp-bislideshow" class="cbp-bislideshow">
<li>
<%= image_tag "blur1.jpg" %>
</li>
<li>
<%= image_tag "blur2.jpg" %>
</li>
</ul>
<% provide(:title, 'Sign Up') %>
<%= form_for(@user) do |f| %>
<div class="sign-up-wrapper divided-wrapper cf">
<div class="left-section">
<h3 class="section-title">JOIN US AND LET'S CHANGE THINGS</h3>
<div class="row">
<div class="w49 pull-left">
<%= f.text_field :firstname, class: "input-text personal ", placeholder: 'FIRSTNAME' %>
</div>
<div class="w49 pull-right">
<%= f.text_field :lastname, class: "input-text personal", placeholder: 'LASTNAME' %>
</div>
<%= f.hidden_field :role, value: :doctor %>
</div>
<div class="row">
<%# f.text_field :specialization, class: "input-text personal ", placeholder: 'SPECIALIZATION' %>
</div>
<%= f.fields_for :doctors do |p| %>
<div class="row">
<%= p.text_field :institution, class: "input-text personal ", placeholder: 'INSTITUTION' %>
</div>
<div class="row">
<%= p.text_field :license_number, class: "input-text personal ", placeholder: 'LICENSE NUMBER' %>
</div>
<% end %>
<span class="remind bottom-message"> ONCE INSIDE DON'T FORGET TO UPDATE YOUR PROFILE WITH MORE DETAILS </span>
</div>
<div class="right-section">
<h3 class="section-title"></h3>
<div class="row">
<%= f.text_field :email, class: "input-text personal ", placeholder: 'EMAIL' %>
</div>
<div class="row">
<%= f.password_field :password, class: "input-text personal ", placeholder: 'PASSWORD' %>
</div>
<div class="row">
<%= f.password_field :password_confirmation, class: "input-text personal ", placeholder: 'CONFIRM PASSWORD' %>
</div>
<div class="row cf">
<%= f.submit class: 'btn-join btn', value: 'JOIN NOW' %>
</div>
<div class="row">
<a href="#" class="btn-flip-social">SIGN UP WITH FACEBOOK / TWITTER ACCOUNT?</a>
</div>
</div>
</div>
<% end %>
Everytimie我执行这些部分,只有用户模型被填充但不是医生表?我的代码有什么问题吗?
修改
将doctors_attributes更改为doctor_attributes 将@ user.doctor.build`改为@ user.build_doctor
在日志中。我看到了这个错误---&gt;未经许可的参数:医生
所以理论上,我认为我们知道问题是什么,但我不知道如何在strong_parameters中解决这个问题。尚未在rails中尝试使用accepted_nested_attributes_for的strong_parameter,这是我的第一次。任何解决方案?
答案 0 :(得分:0)
在user_params
模型中,属性doctors_attributes
应为doctor_attributes
,因为它是has_one
关系。如果是has_many
关系,则为doctors_attributes
。 _attributes
之前的部分将是关联的名称。
另一个注意事项:如果您希望能够从用户表单中更新医生,您还应该在id
数组中加入doctor_attributes
。虽然现在我想到了它,但它可能只是has_many
嵌套关联的要求。我没有尝试过has_one
而不包括id
。
答案 1 :(得分:0)
在fields_for中,替换:医生:医生。请记住,你正在做一对一的关系。