railstutorial.org, Chapter 6. unknown attribute: password

后端 未结 6 700
感动是毒
感动是毒 2021-01-05 14:11

I have finished Chapter 6 of railstutorial but all my User model specs have started failing soon after I added password & password_confirmation

相关标签:
6条回答
  • 2021-01-05 14:45

    Just add has_secure_password into User model file if you haven't done this yet.

    class User < ActiveRecord::Base
     attr_accessible :email, :name, :password, :password_confirmation
     has_secure_password
     # ..
    end
    
    0 讨论(0)
  • 2021-01-05 14:45

    I think the accepted answer, to add attr_accessor :password, :password_confirmation for User model is wrong. Because if has_secure_password creates those attributes virtually, that means it does it for some reason (security). So solution might be to invoke BCrypt wherever you create a User test object:

    require 'bcrypt'
    
        @user = User.new(name: 'test', password: BCrypt::Password.create("my password"), password_confirmation: 'my password')
    

    I wrote about this on this post:

    Rails - Unknown attribute password

    Please correct me if I am wrong.

    0 讨论(0)
  • 2021-01-05 14:49

    I just had the same problem. I was doing this on Cloud9.

    I noticed that my tests weren't all running either.

    Refreshing my file tree fixed both these problems.

    I didn't need to put :password or :password_confirmation directly in the model (or making any coding changes at all from the tutorial.

    0 讨论(0)
  • 2021-01-05 14:50

    It is producing these errors because the very first before {} block is trying to add two attributes that the User model doesn't specify - password and password_confirmation.

    Because this before {} block is run before every test (that's the point), you are getting an error on every test and they're all failing. It's not that the test is failing, per se, but rather ActiveRecord doesn't know what to do with those attributes and is producing an error before each test is really even run:

    ActiveRecord::UnknownAttributeError:
    unknown attribute: password
    

    As codeneko's answer says, this is fixed by simply moving forward in the tutorial and putting has_secure_password in the User model file. This tells ActiveRecord to accept password and password confirmation attributes and all the tests will pass.

    Unfortunately the tutorial promptly tells you to comment out has_secure_password so using diving's answer as a stand-in is probably not a bad idea until the tutorial has you uncomment that.

    0 讨论(0)
  • 2021-01-05 14:57

    I ended up spending a few hours and couldn't find any answers. My problem ended up being on the downcase line:

    before_save { self.email = email.downcase }
    

    He specifies that you can do this line in a different way which I did. See below.

    https://www.railstutorial.org/book/_single-page#sec-creating_and_authenticating_a_user

    In Listing 6.31, we could have written the assignment as self.email = self.email.downcase (where self refers to the current user), but inside the User model the self keyword is optional on the right-hand side: self.email = email.downcase

    Don't do it this way. It gave me error messages every time until I reverted to before_save { self.email = email.downcase }

    and removed: self.email = self.email.downcase

    I'm a noob so not completely sure why this made the difference but it did.

    0 讨论(0)
  • 2021-01-05 14:58

    Add:

    attr_accessor :password, :password_confirmation
    

    The tutorial will then make you save a digest in password_digest

    0 讨论(0)
提交回复
热议问题