简体   繁体   中英

undefined method `digest' with rails 4.0.1

I'm going through Agile Web Development With Rails 4 and have got to the point where I create a user. It's being done like so:

rails generate scaffold User name:string password:digest

But when I try to run rake db:migrate I get the following error: undefined method digest'

What is the cause of that and how can I fix it? I googled that digest is a rails 4 feature, so how come it's an issue?

Edit:

The information below is not entirely correct. It appears as if this generator feature is poorly documented, but does exist. Using password:digest in the generator simply creates a string column in the migration called :password_digest and adds has_secure_password to the model. A tutorial on this feature can be found here .


I see no mention of being able to define a password:digest in the Rails 4 Migrations Guide . Nor do I see it in the actual Rails 4 stable source .

You are correct that this page mentions it as being something you can do, however, it appears as if this feature either got dropped or has not made it into Rails 4 stable yet.

As far as fixing it, it appears that in Rails 4 stable these are the fields you can define with a migration:

t.column
t.index
t.rename_index
t.timestamps
t.change
t.change_default
t.rename
t.references
t.belongs_to
t.string
t.text
t.integer
t.float
t.decimal
t.datetime
t.timestamp
t.time
t.date
t.binary
t.boolean
t.remove
t.remove_references
t.remove_belongs_to
t.remove_index
t.remove_timestamps

You will probably have to store the password as a string (but don't mistake this as me saying "store the plain text password as a string" (!) ... you still want to at least hash it and probably salt it.) This video should provide a lot of help with doing this in Rails: Authentication in Rails .

Authentication is a complex issue, and may require some research to do it properly, but it will be well worth it. I would highly recommend first watching that video and getting a basic understanding of user auth in Rails, and then possibly looking into some hardened gems that you can use. Using an existing gem has both the benefit of being fully-featured and also being battle-hardened by the open source community. If you find the right gem that fits your needs and is well reviewed and actively maintained, it will usually be better than what you can build yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM