【问题标题】:Can't mass-assign protected attributes: time_zone无法批量分配受保护的属性:time_zone
【发布时间】:2013-12-23 22:06:47
【问题描述】:

我在尝试提交我的 simple_form 时收到此错误:

= simple_form_for(@profile, :url => profile_path, :method => :put) do |f|
  = f.error_notification
  .container
    = f.input :time_zone, :collection => ActiveSupport::TimeZone.all, 
      :label_method => :to_s, :value_method => :name, :include_blank => false
  .actions
    = f.button :submit

我已将time_zone 放入可访问属性中:

class Profiles < ActiveRecord::Base
  attr_accessible :time_zone

  belongs_to :user
end

并将列添加到表中:

class AddTimeZoneToProfile < ActiveRecord::Migration
  def change
    add_column :profiles, :time_zone, :string, :default => "UTC"
  end
end

并运行迁移:

$rake db:migrate
==  AddTimeZoneToProfile: migrating ===========================================
-- add_column(:profiles, :time_zone, :string, {:default=>"UTC"})
   -> 0.0369s
==  AddTimeZoneToProfile: migrated (0.0370s) ==================================

由于某种原因,我得到了批量分配错误。关于这里有什么问题的任何线索?

更新: 这些是随更新请求发送的参数:

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"udKomXmmmOG+7Z4YKR03y9zMg58rnx1EXqE33a+6Shw=", "profile"=>{ "time_zone"=>"Stockholm"}, "commit"=>"Update Profile", "action"=>"update", "controller"=>"profiles"}

谢谢,

【问题讨论】:

  • 你在使用 Rails 4 吗?
  • $rails -v Rails 3.2.14 和 ruby​​ $ruby -v ruby​​ 2.0.0p353(2013-11-22 修订版 43784)[x86_64-darwin13.0.0]
  • 尝试重启你的服务器
  • 谢谢,但重启并没有改善。顺便说一句,我正在使用 WEBrick。
  • 旁白:class Profiles ?那应该是单数:class Profile

标签: mysql ruby-on-rails forms activerecord


【解决方案1】:

感谢评论者,我发现我有两个模型文件:profile.rbprofiles.rb,第一个是我应该拥有的,复数文件是错误的。我将attr_accessible 添加到第二个。当然也行不通。

愚蠢的错误总是会产生愚蠢的错误,

谢谢。

【讨论】:

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