2012-04-16 90 views
3

我正沿着Lynda.com上的Ruby on Rail 3 Essential Training教程進行操作。我在創建活動記錄條目時遇到困難。這是我在控制檯中遇到的錯誤。ActiveModel :: MassAssignmentSecurity ::錯誤:無法批量分配受保護的屬性:

1.9.3p125 :007 > user = User.new(:first_name => "Mike", :last_name => "Jones") 
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: first_name, last_name 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes' 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal' 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize' 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment' 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/attribute_assignment.rb:75:in `assign_attributes' 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/base.rb:498:in `initialize' 
    from (irb):7:in `new' 
    from (irb):7 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start' 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start' 
    from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>' 
    from script/rails:6:in `require' 
    from script/rails:6:in `<main>` 

這是我在我的模型:

class User < ActiveRecord::Base 
    attr_accessible :first_name, :last_name 
end 

我在做什麼錯。我有導軌3.2.3

回答

4

嘗試重新啓動控制檯。如果您在控制檯啓動後爲用戶創建了模型,則應重新啓動它。

+0

謝謝你的工作!沒有意識到這很簡單。 – 2012-04-17 01:16:35

+1

重新加載!在控制檯應該重新加載控制檯,所以你不必重新啓動它 – ejunker 2012-10-03 00:56:16

10

據我所知,lynda課程是在rails3和rails 3.2.3上開發的,默認情況下沒有質量分配。你必須去你的模型並添加attr_accessible:name,:position,:visible。基本上你必須添加你想批量分配的每個屬性。

+0

非常感謝Thats工作 – 2012-04-27 19:55:27

2

沒有任何預防措施批量分配允許攻擊者設置任何數據庫列的值,因此默認情況下它已被禁用。

def signup 
    params[:user] # => {:name => 「ow3ned」, :admin => true} 
    @user = User.new(params[:user]) 
end 

詳細描述在Ruby On Rails Security Guide

2

我剛剛將attr_accessible:first_name,:last_name,:username行添加到模型文件中。 這對我有用。

0

確保將attr_accessible :first_name, :last_name置於用戶模型中,而不是在控制器中。

2

我太沿着教程的Ruby on從Lynda.com軌道(3)的基本訓練之後,如果任何人有同樣的問題這裏是爲我工作,

關閉安全設置。打開config/application.rb並將config.active_record.whitelist_attributes更改爲false而不是true。這會讓您的應用程序不太安全,但可以讓您快速地繼續學習本教程。 這是從:http://www.lynda.com/Ruby-on-Rails-3-tutorials/essential-training/55960-2/faqs

相關問題