2011-10-23 26 views
9

Tôi đã chơi xung quanh với has_secure_password và tôi gặp phải sự cố. Thử nghiệm của tôi cho hành động tạo trong UsersController của tôi không phải là làm việc . Vì vậy, tôi bắt đầu chơi xung quanh trong giao diện điều khiển và nhận ra rằng mật khẩu đã không được chuyển đổi thành có và được lưu trong trường password_digest.Rails 3.1.1 has_secure_password digest không được để trống

Khi tôi cố gắng tạo người dùng từ bảng điều khiển, điều sau sẽ xảy ra.

irb(main):031:0> u = User.new(:email => "[email protected]", :password => "test", :password_confirmation => "test") 
=> #<User id: nil, email: "[email protected]", password_digest: nil, created_at: nil, updated_at: nil> 
irb(main):032:0> u.save 
=> false 
irb(main):033:0> u.errors 
=> #<ActiveModel::Errors:0x00000100cde500 @base=#<User id: nil, email: "[email protected]", password_digest: nil, created_at: nil, updated_at: nil>, @messages={:password_digest=>["can't be blank"]}> 

Tôi không chắc mình đang làm gì sai. Có vẻ như thuộc tính password_digest không bao giờ được gán. Nếu tôi tạo một đối tượng người dùng không có thuộc tính và gán từng thuộc tính riêng lẻ thì tôi sẽ gặp lỗi tương tự.

Đây là mô hình của tôi

class User < ActiveRecord::Base 
    has_secure_password 
    attr_accessible :email, :password, :password_confirmation 
end 

Thanks for the help trước.

Alex Shenoy

Trả lời

0

Tôi đã tạo một ví dụ đơn giản và hoạt động như mong đợi. Có lẽ bạn có một cái gì đó trong cấu hình của bạn đó là rối tung với điều này. thử nghiệm của tôi rất đơn giản:

$ rails new test 
$ cd test 
$ rails g model user name:string password_digest:string 
$ rake db:migrate 
$ vim app/model/user.rb 

thêm

has_secure_password 
attr_accessible :name, :password, :password_confirmation 

lưu và thoát

$ rails c 

Loading development environment (Rails 3.1.1) 
ruby-1.9.2-p290 :001 > u = User.new(:name => "[email protected]", :password => "test", :password_confirmation => "test") 
=> #<User id: nil, name: "[email protected]", password_digest: "$2a$10$08xY7p.8hq1.95ZQsx63ku05YfvVqSQ/CLiqUW5dtGhZ...", created_at: nil, updated_at: nil> 
ruby-1.9.2-p290 :002 > u.save 
Binary data inserted for `string` type on column `password_digest` 
    SQL (51.4ms) INSERT INTO "users" ("created_at", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sun, 23 Oct 2011 13:38:49 UTC +00:00], ["name", "[email protected]"], ["password_digest", "$2a$10$08xY7p.8hq1.95ZQsx63ku05YfvVqSQ/CLiqUW5dtGhZSP9S7FtUy"], ["updated_at", Sun, 23 Oct 2011 13:38:49 UTC +00:00]] 
=> true 
10

tôi đã có triệu chứng tương tự, một thông điệp rằng password_digest không thể để trống. Vấn đề tôi đã là tôi đã thiết lập này trong user.rb:

attr_accessor :password 

này ngăn cản mật khẩu method = khỏi bị gọi (xem secure_password.rb)

# Encrypts the password into the password_digest attribute. 
    def password=(unencrypted_password) 
    @password = unencrypted_password 
    unless unencrypted_password.blank? 
     self.password_digest = BCrypt::Password.create(unencrypted_password) 
    end 
    end 

và do đó giá trị là không bao giờ đặt cho password_digest.

Đối với tôi, bản sửa lỗi là xóa dòng attr_accessor.

2

Tôi gặp vấn đề tương tự. Tôi sẽ lưu người dùng, nhưng password_digest luôn là nil. Đối với tôi đó là vì tôi đã có lệnh mã sau vào mô hình của tôi:

attr_accessible :name, :password, :password_confirmation 
has_secure_password 

tôi đã thay đổi nó để:

has_secure_password 
attr_accessible :name, :password, :password_confirmation 

Bây giờ nó hoạt động hoàn hảo.

Các vấn đề liên quan