1

所以這是我的問題。我一直在和我在項目開始時創建的用戶合作一個月。今天筆者從sqllite切換到SQLSERVER,以滿足客戶的需求,我去的時候用我的註冊表單創建我有以下錯誤的新用戶:保存用戶時不能將符號轉換爲整數錯誤

can't convert Symbol into Integer 

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"51nF50CYGNqz3N4o7TUYSyWeTadulXojQBPqERjvlcY=", 
"user"=>{ 
    "email"=>"[email protected]", 
    "login"=>"bgarrison", 
    "password"=>"[FILTERED]", 
    "password_confirmation"=>"[FILTERED]", 
    "profile_attributes"=>{ 
    "prefix"=>"", 
    "first_name"=>"Bill", 
    "last_name"=>"Garrison", 
    "suffix"=>"", 
    "birthday"=>"1983-06-01", 
    "phone_numbers_attributes"=>{ 
     "0"=>{ 
     "info"=>"1234567890", 
     "label"=>"Cell" 
     } 
    } 
    } 
}, 
"commit"=>"Register"} 

我有一種感覺,在某些時候我搞砸了註冊過程,但我不能爲我的生活弄清楚在哪裏。用戶 - > has_one配置文件 - > has_many phone_numbers。

用戶控制器:

def create  
    @user = User.new(params[:user]) 

    if @user.save 
     @profile = @user.profile 
     flash[:notice] = "Your account has been created." 
     redirect_to(@user) 
    else 
     flash[:notice] = "There was a problem creating you." 
     render :action => :new, :layout => 'logged_out' 
    end 
    end 

用戶模型:

class User < ActiveRecord::Base 
    # Accessible attributes 
    attr_accessible :login, 
    :email, 
    :password, 
    :password_confirmation, 
    :profile_attributes, 
    :active 

    # Associations 
    has_one :profile, dependent: :destroy, autosave: true 

    # Allows for a profile hash in user creation (stored in :profile_attributes) 
    accepts_nested_attributes_for :profile 

資料型號:

class Profile < ActiveRecord::Base 
    # Accessible Attributes 
    attr_accessible :birthday, 
    :company_id, 
    :first_name, 
    :last_name, 
    :prefix, 
    :suffix, 
    :phone_numbers_attributes, 
    :addresses_attributes 

    # Model Associations 
    has_many :phone_numbers, :as => :contactable, :class_name => "PhoneNumber", autosave: true 
    accepts_nested_attributes_for :phone_numbers, allow_destroy: true, reject_if: :all_blan 

任何幫助,將不勝感激。謝謝!

更新:1另外,我還測試了一些,並意識到如果我離開了手機號碼,然後它工作.....如果我再更新使用相同的形式和添加電話號碼,一切工作正常。

+0

可以包括在錯誤發生的代碼? – jvnill 2013-03-12 14:44:12

+0

我沒有....其創建方法的第一行:@user = User.new(PARAMS [:用戶]) – 2013-03-12 14:45:52

+0

噢我還忘了提及,我有:accepts_nested_attributes_for:PHONE_NUMBERS,allow_destroy:真,reject_if: :all_blank – 2013-03-12 14:47:05

回答

0

於是,經過敲我的頭靠在牆上的一兩天,我終於想通了這一點。但要理解它,我需要解釋我的模型更好一點。

基本上,從上面可以看到,用戶具有通過一個多態型的關聯有許多PHONE_NUMBERS和地址的簡檔(:爲=>:接觸)。但是,可聯繫實際上是一個名爲ContactInformation的基類,它使用STI來包含所有類型的可聯繫信息。

有一次,我決定,地址有4名額外的字段被塞滿了STI的關係,但我還是想繼續保持。我的解決方案是將所有這些字段序列化到ContactInformation的「信息」字段。現在,電話號碼只有「數字」作爲一個字段被序列化並存儲到「信息」中,但是如果我想將它分成「區號」「擴展名」等,則實現將變得簡單。

這導致的問題。在我的註冊表單上,我使用標籤/信息作爲我的phone_number字段而不是標籤/號碼。我編輯過我的編輯表單,但不是我的新表單(是的,我知道它們應該是同一個,但我有一個特殊的Ajax表單進行編輯)。

這裏是

class ContactInformation < ActiveRecord::Base 

    attr_accessible :contactable_id, :contactable_type, :info, :label, :type 
    belongs_to :contactable, :polymorphic => true 

end 

class PhoneNumber < ContactInformation 
    attr_accessible :number 
    stash :number, in: :info 

    #----------------------------------Validations--Start------------------------- 
    validates :number, presence: true 
    #----------------------------------Validations--End--------------------------- 
end 

class Address < ContactInformation 
    attr_accessible :street_address, :city, :state, :postal 
    stash :street_address, :city, :state, :postal, in: :info 

    #----------------------------------Validations--Start------------------------- 
    validates :street_address, :city, :state, :postal, presence: true 
    #----------------------------------Validations--End--------------------------- 
end 
1

嵌套屬性應該傳遞作爲陣ContactInformation/******中國/地址代碼:

"user"=>{ 
    "email"=>"[email protected]", 
    "login"=>"bgarrison", 
    "password"=>"[FILTERED]", 
    "password_confirmation"=>"[FILTERED]", 
    "profile_attributes"=>[ 
    { 
     "prefix"=>"", 
     "first_name"=>"Bill", 
     "last_name"=>"Garrison", 
     "suffix"=>"", 
     "birthday"=>"1983-06-01", 
     "phone_numbers_attributes"=>{ 
     "0"=>{ 
      "info"=>"1234567890", 
      "label"=>"Cell" 
     } 
     } 
    } 
    ] 
} 
相關問題