当我提交表单时,它正在{"utf8"=>"✓", "authenticity_token"=>"blah", "client"=>{"first_name"=>"jack", "last_name"=>"kool","complaints"=>{"symptom"=>"burnt"}
中传递参数并且我得到Unpermitted Parameters: complaints
由于投诉嵌套在客户端中,它应该通过complaints_attributes
,就像我设置的那样在强大的障碍中,我无法弄清楚它为什么不是。
class ClientsController < ApplicationController
def new
@client = Client.new
end
def edit
@client = Client.find(params[:id])
end
def create
@client = Client.new(client_params)
if @client.save
redirect_to @client
else
render 'new'
end
end
def update
@client = Client.find(params[:id])
if @client.update(client_params)
redirect_to @client
else
render 'edit'
end
end
private
def client_params
params.require(:client).permit(:first_name, :last_name, complaints_attributes: [ :symptom, :id ])
end
end
客户端模型:
class Client < ActiveRecord::Base
has_many :complaints
has_one :personal_disease_history
has_one :family_disease_history
has_many :surgeries
has_many :hospitalizations
has_many :medications
has_many :allergies
validates :first_name, :last_name, presence: true
accepts_nested_attributes_for :complaints
end
投诉模型:
class Complaint < ActiveRecord::Base
belongs_to :client
end
形式:
<%= form_for :client, url: clients_path, html: { class: "form-inline" } do |f| %>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Health History Form</h3>
</div>
<div class="panel-body">
<div class="form-blocks"><legend>Name</legend>
<div class="form-group">
<%= f.label :first_name, class: "sr-only" %>
<%= f.text_field :first_name, class: "form-control", placeholder: "First Name" %>
</div>
<div class="form-group">
<%= f.label :last_name, class: "sr-only" %>
<%= f.text_field :last_name, class: "form-control", placeholder: "Last Name" %>
</div>
</div>
<div class="form-blocks"><legend>Complaints</legend>
<div class="form-group">
<%= f.fields_for :complaints do |builder| %>
<%= builder.label :symptom, class: "sr-only" %>
<%= builder.text_field :symptom, class: "form-control", placeholder: "Symptom" %>
<% end %>
</div>
</div>
<div>
<%= f.submit "Submit Form", class: "btn btn-primary btn-lg"%>
</div>
<% end %>
答案 0 :(得分:0)
你需要build
相关模型
def new
@client = Client.new
@client.complaints.build
end
答案 1 :(得分:0)
在客户端控制器内部创建操作,尝试在if @ client.save:
中说明这一点Complaint.create(symptom: params[
"client"]["complaints"]["symptom"], client_id: @client.id)
未经许可的参数与嵌套属性问题有关,但这应该可以获得实际的记录保存。如果没有这样做,请发布提交周围的堆栈跟踪。
还有其他一些想法:但是(1)你在允许的参数中有抱怨_属性,但是你可以将其改为抱怨那些在参数中发生的事情。