class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
schema.rb :
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
在rails控制台中执行: user = User.new(name:"ygx", email:"3333@qq.com",password:"q",password_confirmation:"q")
报错信息如下:
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: password, password_confirmation
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/activemodel-3.2.12/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/activemodel-3.2.12/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/activemodel-3.2.12/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/activemodel-3.2.12/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.12/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.12/lib/active_record/base.rb:497:in `initialize'
from (irb):26:in `new'
from (irb):26
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /Users/ygx/.rvm/gems/ruby-2.0.0-p0/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
根据报错信息搜了很多,这个报错不少都是因为没有 attr_accessible :name, :email, :password, :password_confirmation。不知道为什么我还是有问题。 在没加password之前(rb和db里都没加,只有name和email),用new是可以创建用户的。 这是为什么呢,憋了一天了,求助。。。
3 个解决方案
#1
first, make sure password & password_confirmation in your users table.
I think these two fields doesn't exist in your users table through check your schema file. your problem is that there's no these two fields in your User model.
second, add attr_accessible field1,field2, which you want to modify.
or you can close this attr protected function, through application.rb file.
check this line
config.active_record.whitelist_attributes = true|false
true is enable attr protect,
false means disable this function.
I think these two fields doesn't exist in your users table through check your schema file. your problem is that there's no these two fields in your User model.
second, add attr_accessible field1,field2, which you want to modify.
or you can close this attr protected function, through application.rb file.
check this line
config.active_record.whitelist_attributes = true|false
true is enable attr protect,
false means disable this function.
#2
你寫的,我完全用到。
你之控制臺,改後是否有重新開啟。或是有否reload!
你之控制臺,改後是否有重新開啟。或是有否reload!
#3
勿亂講一通,你要留意人家用了has_secure_password。
在情在理,password不會直接記在database。password_confirmation更不會。
在情在理,password不會直接記在database。password_confirmation更不會。
#1
first, make sure password & password_confirmation in your users table.
I think these two fields doesn't exist in your users table through check your schema file. your problem is that there's no these two fields in your User model.
second, add attr_accessible field1,field2, which you want to modify.
or you can close this attr protected function, through application.rb file.
check this line
config.active_record.whitelist_attributes = true|false
true is enable attr protect,
false means disable this function.
I think these two fields doesn't exist in your users table through check your schema file. your problem is that there's no these two fields in your User model.
second, add attr_accessible field1,field2, which you want to modify.
or you can close this attr protected function, through application.rb file.
check this line
config.active_record.whitelist_attributes = true|false
true is enable attr protect,
false means disable this function.
#2
你寫的,我完全用到。
你之控制臺,改後是否有重新開啟。或是有否reload!
你之控制臺,改後是否有重新開啟。或是有否reload!
#3
勿亂講一通,你要留意人家用了has_secure_password。
在情在理,password不會直接記在database。password_confirmation更不會。
在情在理,password不會直接記在database。password_confirmation更不會。