我在嘗試將數據插入到具有涉及兩個模型的複雜形式的數據庫時遇到問題,並且我搜索了Stackoverflow上的每個帖子以找到最佳答案,但是我無法靠近任何地方靠近。試圖爲具有多態關聯的模型插入數據
在我的模型
class ClientIndividual < ActiveRecord::Base
has_one :client_assignment, :as => :clientassignmentable
accepts_nested_attributes_for :client_assignment
end
class ClientAssignment < ActiveRecord::Base
belongs_to :clientassignmentable, :polymorphic => true
end
在我ClientIndividual控制器
class ClientIndividualsController < ApplicationController
before_action :set_client_individual, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
def new
@client_individual = ClientIndividual.new
end
def create
@client_individual = ClientIndividual.new(client_individual_params)
respond_to do |format|
if @client_individual.save
format.html { redirect_to @client_individual, notice: 'Client individual was successfully created.' }
format.json { render action: 'show', status: :created, location: @client_individual }
else
format.html { render action: 'new' }
format.json { render json: @client_individual.errors, status: :unprocessable_entity }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client_individual
@client_individual = ClientIndividual.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_individual_params
params.require(:client_individual).permit(:title, :first_name, :middle_name,
:last_name, :date_of_birth, :work_phone, :home_phone, :mobile_phone, :email_address,
:preferred_contact_type, :residential_address, :residential_suburb, :residential_state,
:residential_postcode, :same_as_residential, :postal_address, :postal_suburb,
:postal_state, :postal_postcode, :emergency_contact_person, :emergency_phone_no,
:industry_type, :has_referral, :notes, clientassignmentable_attributes:
[:assignment_types_id, :employees_id, :start_date, :manager_id, :client_type])
end
end
在我Client_Individual#新觀點
<%= form_for(@client_individual) do |f| %>
<div class="add_data_section">
<%= f.fields_for (:clientassignmentable) do |client_assignment_form| %>
........
........
<div class="row_container">
<div class="field 1" style="width: 409px;">
<%=client_assignment_form.label :assignment_types_id, "Assignment Type*"%>
</div>
<div class="dropdown">
<label>
<%= select('client_individual[clientassignmentable]', 'assignment_types_id', AssignmentType.all.collect {|assignment_type| [assignment_type.name, assignment_type.id ] }, {prompt: 'Select Assignment Type'}, :style =>"width: 264px;")%>
</label>
</div>
</div>
<div class="row_container">
<div class="field " style="width: 409px;">
<%=client_assignment_form.label :start_date, "Assignment Start Date"%>
</div>
<div class="inputfield">
<%=client_assignment_form.text_field :start_date, :class=>'assignmentdatepicker', :style=>'width: 260px;'%>
</div>
</div>
<div class="row_container">
<div class="field " style="width: 409px;">
<%=client_assignment_form.label :manager_id, "Manager Appointed*"%>
</div>
<div class="dropdown">
<label>
<%= select('client_individual[clientassignmentable]', 'manager_id', Employee.all.select{ |manager| manager.staff_rank_id == 2}.collect{|employee| [employee.first_name + " " + employee.last_name, employee.id ] }, {prompt: 'Select ManagerList'}, :style =>"width: 264px;")%>
</label>
</div>
</div>
<div class="row_container">
<div class="field 1" style="width: 409px;">
<%=client_assignment_form.label :employees_id, "Staff Appointed*"%>
</div>
<div class="dropdown">
<label>
<%= select('client_individual[clientassignmentable', 'employees_id', Employee.all.select{ |employee| employee.user_id == current_user_id}.collect{|employee| [employee.first_name + " " + employee.last_name, employee.id ] }, {prompt: 'Select CurrentUser'}, :style =>"width: 264px;")%>
</label>
</div>
</div>
<%=client_assignment_form.hidden_field :client_type%>
<% end %>
......
<% end %>
所以我已經是,我得到了一個ClientIndividual模型有一個ClientAssignment模型。因此,ClientAssignment模型具有ClientIndividual FK,因此在創建新的ClientIndividual數據時,我期望在表單上生成新的ClientAssignment數據。因此,在我的控制,我宣佈我的強烈PARAMS像這樣
clientassignmentable_attributes: [:assignment_types_id, :employees_id, :start_date, :manager_id, :client_type]
但我點擊保存新的數據,什麼也沒有插入到數據庫...
然後檢查我development.log和它說下列。
Started POST "/client_individuals" for 127.0.0.1 at 2014-03-30 19:58:52 +1100
Processing by ClientIndividualsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0Z+BJcjQE7oa+uzdo/2sKRPiV01EQDXfedMkLE7pTjg=", "groups"=>{"id"=>"1"}, "client_individual"=>{"clientassignmentable"=>{"assignment_types_id"=>"1", "start_date"=>"03/31/2014", "manager_id"=>"13", "employees_id"=>"25", "client_type"=>""}, "title"=>"t", "first_name"=>"t", "middle_name"=>"t", "last_name"=>"t", "date_of_birth"=>"", "work_phone"=>"", "home_phone"=>"", "mobile_phone"=>"", "email_address"=>"[email protected]", "residential_address"=>"t", "residential_suburb"=>"t", "residential_state"=>"t", "residential_postcode"=>"t", "same_as_residential"=>"0", "postal_address"=>"t", "postal_suburb"=>"t", "postal_postcode"=>"t", "emergency_contact_person"=>"", "emergency_phone_no"=>"22223", "industry_type"=>"", "notes"=>""}, "CREATE"=>"Create Client"}
[1m[36mUser Load (0.7ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1[0m
Unpermitted parameters: clientassignmentable
[1m[35m (0.3ms)[0m BEGIN
[1m[36m (0.2ms)[0m [1mROLLBACK[0m
我不斷收到這個不允許的參數:clientassignmentable。但是我已經在我的控制器列入白名單作爲強大的參數之一,所以它爲什麼抱怨?
當然,如果你知道擺在你面前的正確關聯是什麼,那麼這個問題不難確定嗎?
我還能遺漏什麼?!?!我一直在這個一整天,看起來很迷茫....
您是不是指fields_for:client_assignment,而不是form_for:client_assignment?原因我需要主窗體來保存新的Client Individual表單。然而,後來,我從另一篇文章中找到了解決方案,建議我應該這樣做:012_ fields_for:client_assignment,ClientAssignment.new do | client_assignment_form | 原因如果我只做fields_for:client_assignment,嵌套屬性將不會呈現。因爲它在創建ClientIndividual時認爲ClientAssignment是無類。 – awongCM
@awongCM很棒,抓住了答案。 –