新手问题 请教个问题

fatbigbright · December 12, 2013 · Last by shulin replied at December 12, 2013 · 1968 hits

环境:rails 3.2.12 + ruby 1.9.3p392 重现步骤:

  1. rails g model order number:string description:text quantity:integer建立一个 model,
  2. bundle exec rake db:migration更新数据库
  3. 使用rails c在控制台中增加一条 order 数据。
  4. rails g migration add_type_to_order为 order 表增加一个 type 字段。
  5. bundle exec rake db:migration再次更新数据库
  6. rails c的控制台中使用 order = Order.first order.type = 'C' order.save 这时发现 order 可以正常更新 继续使用 order.update_attributes( :type => "M" )来更新,发现报错“ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: type”,上网查说“type”是个比较敏感的词,于是改成了“order_type”,结果发现仍然不能使用 update_attributes 方法来更新这个字段的值,向各位前辈求教这是为什么呢?先行谢过了~

查了一下 Model 下的 order.rb 文件,发现 order_type 没有加入到 attr_accessible 中,难道增加字段默认不会把新增字段加入到这里吗?还有,为什么 update_attributes 不能工作,但直接用order.order_type="X"可以正常赋值呢?

5 Floor has deleted
You need to Sign in before reply, if you don't have an account, please Sign up first.