【问题标题】:can't convert symbol into integer error when saving user保存用户时无法将符号转换为整数错误
【发布时间】:2013-03-12 14:37:36
【问题描述】:

所以这是我的问题。一个月以来,我一直在与我在项目开始时创建的用户合作。今天我从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"}

我有一种感觉,在某些时候我搞砸了注册过程,但我终生无法弄清楚在哪里。用户-> has_one profile-> 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 此外,我已经测试了一些,并意识到如果我遗漏了电话号码,那么它就可以工作......如果我使用相同的表格进行更新并添加一个电话号码,一切都会正常工作。

【问题讨论】:

  • 你能把出错的代码包含进去吗?
  • 我做到了....它是 create 方法的第一行:@user = User.new(params[:user])
  • 哦,我也忘了提到我有:accept_nested_attributes_for :phone_numbers, allow_destroy: true, reject_if: :all_blank
  • 对不起。不知道是什么导致了这个问题。点赞
  • 伙计,这变得更麻烦了。我没有进行任何更改,现在错误已随机更改为“未知属性:密码”...通过...发送相同的参数....相同的控制器和模型....奇怪的编辑:那不是真的....我有实际上安装了调试器......也许它给了我更具体的东西?

标签: ruby-on-rails sql-server ruby-on-rails-3 activerecord rails-activerecord


【解决方案1】:

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

"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"
        }
      }
    }
  ]
}

【讨论】:

    【解决方案2】:

    所以,经过几天的头撞墙后,我终于想通了。但是要理解它,我需要更好地解释我的模型。

    基本上,从上面您可以看到用户的个人资料通过多态关联(:as => :contactable)具有许多电话号码和地址。但是,contactable 实际上是一个名为 ContactInformation 的基类,它使用 STI 来包含所有类型的可联系信息。

    在某一时刻,我认为有 4 个额外的地址字段会使 STI 关系变得混乱,但我仍然想保留它。我的解决方案是将所有这些字段序列化到 ContactInformation 的“信息”字段中。现在,电话号码只有“号码”作为序列化并存储到“信息”中的字段,但如果我想将它分成“区号”“分机”等,实现将很简单。

    这会导致问题。在我的注册表单上,我在 phone_number 字段中使用了标签/信息,而不是标签/号码。我编辑了我的编辑表单,但没有编辑我的新表单(是的,我知道它们应该是相同的,但我有一个特殊的 ajax 表单用于编辑)。

    这里是 ContactInformation / PhoneNumber / Address 的代码

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      相关资源
      最近更新 更多