Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails can';保存用户时,无法将符号转换为整数错误_Ruby On Rails_Sql Server_Ruby On Rails 3_Activerecord_Rails Activerecord - Fatal编程技术网

Ruby on rails can';保存用户时,无法将符号转换为整数错误

Ruby on rails can';保存用户时,无法将符号转换为整数错误,ruby-on-rails,sql-server,ruby-on-rails-3,activerecord,rails-activerecord,Ruby On Rails,Sql Server,Ruby On Rails 3,Activerecord,Rails Activerecord,这就是我的问题。我已经和我在项目开始时创建的用户一起工作了一个月了。今天,为了满足客户要求,我从sqllite切换到sqlserver,当我使用注册表创建新用户时,出现以下错误: can't convert Symbol into Integer Parameters: {"utf8"=>"✓", "authenticity_token"=>"51nF50CYGNqz3N4o7TUYSyWeTadulXojQBPqERjvlcY=", "user"=>{ "ema

这就是我的问题。我已经和我在项目开始时创建的用户一起工作了一个月了。今天,为了满足客户要求,我从sqllite切换到sqlserver,当我使用注册表创建新用户时,出现以下错误:

can't convert Symbol into Integer

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"51nF50CYGNqz3N4o7TUYSyWeTadulXojQBPqERjvlcY=",
 "user"=>{
   "email"=>"test@blizzardlabs.com",
   "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"}
我有一种感觉,在某种程度上,我把注册过程搞砸了,但我一辈子都不知道在哪里。用户->拥有一个个人资料->拥有多个电话号码

用户控制器:

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用户
配置文件模型:

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
类配置文件:可联系,:class\u name=>“PhoneNumber”,自动保存:true
接受:电话号码的\u嵌套\u属性\u,允许\u销毁:true,拒绝\u如果::全部\u blan
任何帮助都将不胜感激。谢谢


更新:1此外,我已经测试了一些,并意识到如果我省略了电话号码,那么它就工作了……如果我使用相同的表单进行更新并添加电话号码,那么一切都正常。

因此,经过几天的碰壁,我终于明白了这一点。然而,为了理解它,我需要更好地解释我的模型

基本上,从上面可以看到,用户有一个通过多态关联(:as=>:contactable)拥有许多电话号码和地址的配置文件。然而,contactable实际上是一个名为ContactInformation的基类,它使用STI来包含所有类型的contactable信息

有一次,我决定为地址添加4个额外字段,这会使STI关系变得混乱,但我仍然想保留它。我的解决方案是将所有这些字段序列化到ContactInformation的“info”字段中。现在,电话号码只有“number”作为一个字段,该字段被序列化并存储到“info”中,但是如果我想将其拆分为“区号”、“扩展名”等,那么实现将非常简单

这就导致了这个问题。在我的登记表上,我使用标签/信息作为我的电话号码字段,而不是标签/号码。我编辑了我的编辑表单,但没有编辑我的新表单(是的,我知道它们应该是同一个表单,但我有一个特殊的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
class ContactInformationtrue
结束
类别PhoneNumber<联系人信息
可访问属性:编号
隐藏:编号,在::info中
#----------------------------------验证--启动-------------------------
验证:编号,状态:真
#----------------------------------验证--结束---------------------------
结束
类地址<联系人信息
可访问属性:街道地址::城市,:州,:邮政
藏匿:街道\地址,:城市,:州,:邮政,地址::信息
#----------------------------------验证--启动-------------------------
验证:街道地址,:城市,:州,:邮政,状态:true
#----------------------------------验证--结束---------------------------
结束

所以,在头撞墙几天后,我终于明白了这一点。然而,为了理解它,我需要更好地解释我的模型

基本上,从上面可以看到,用户有一个通过多态关联(:as=>:contactable)拥有许多电话号码和地址的配置文件。然而,contactable实际上是一个名为ContactInformation的基类,它使用STI来包含所有类型的contactable信息

有一次,我决定为地址添加4个额外字段,这会使STI关系变得混乱,但我仍然想保留它。我的解决方案是将所有这些字段序列化到ContactInformation的“info”字段中。现在,电话号码只有“number”作为一个字段,该字段被序列化并存储到“info”中,但是如果我想将其拆分为“区号”、“扩展名”等,那么实现将非常简单

这就导致了这个问题。在我的登记表上,我使用标签/信息作为我的电话号码字段,而不是标签/号码。我编辑了我的编辑表单,但没有编辑我的新表单(是的,我知道它们应该是同一个表单,但我有一个特殊的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
class ContactInformationtrue
结束
类别PhoneNumber<联系人信息
可访问属性:编号
隐藏:编号,在::info中
#----------------------------------验证--启动-------------------------
验证:编号,状态:真
#----------------------------------验证--结束---------------------------
结束
类地址<联系人信息
可访问属性:街道地址::城市,:州,:邮政
藏匿:街道\地址,:城市,:州,:邮政,地址::信息
#----------------------------------验证--启动-------------------------
验证:街道地址,:城市,:州,:邮政,状态:true
#----------------------------------验证--结束---------------------------
结束

嵌套属性应作为数组传入:

"user"=>{
  "email"=>"test@blizzardlabs.com",
  "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"
        }
      }
    }
  ]
}

嵌套属性应作为数组传入:

"user"=>{
  "email"=>"test@blizzardlabs.com",
  "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"
        }
      }
    }
  ]
}
可以