2014-03-30 55 views
1

我在嘗試將數據插入到具有涉及兩個模型的複雜形式的數據庫時遇到問題,並且我搜索了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。但是我已經在我的控制器列入白名單作爲強大的參數之一,所以它爲什麼抱怨?

當然,如果你知道擺在你面前的正確關聯是什麼,那麼這個問題不難確定嗎?

我還能遺漏什麼?!?!我一直在這個一整天,看起來很迷茫....

回答

0

你應該寫

fields_for :client_assignment 

,並改變這種因此在其他地方使用:clientassignmentable

進一步閱讀:
http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

解決與領域中的新形式不使所述問題,控制器,你需要做的

@client_individual.build_client_assignment 
+1

您是不是指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

+0

@awongCM很棒,抓住了答案。 –

0

我想,我沒有出錯,這.....

它的閱讀設置多態關聯的基礎知識,經過這麼尷尬,我意識到自己的多態性協會是完全完全錯誤!由於這個原因,當我清楚地知道我的數據庫設計的初始步驟完全錯誤時,我浪費了將近2天的時間來試圖保存表單的數據!多麼徹底的浪費!

ARRRGGGH !!!!!

現在我需要回到繪圖板,找到如何恢復它!

+0

你浪費了一段時間,但希望你也學到了一些東西。經常閱讀文檔是很好的,會爲你節省一些頭痛。 –

+1

謝謝邁克爾。的確我應該。在開始使用代碼之前,我需要養成這樣做的習慣。在做任何實際的代碼之前,我應該練習閱讀/練習基本知識的習慣。否則,我將在未來再次遇到同樣的問題,它肯定看起來不太好...... – awongCM