Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method `password_hash' error with datamapper
    text
    copied!<p>I'm trying to build [Ryan Bates' "authenticaion from scratch"][1] using sinatra but authentication does not work. I get a "undefined method password_hash" error.</p> <p>Everything else is working. I checked datamapper documentation, especially for lazy loading, but couldn't find anything useful. The code is below. What mistake am I doing?</p> <h1>main.rb (only relevant part)</h1> <pre><code>post "/login" do user = User.authenticate params[:email], params[:password] if user session[:user_id] = user.id redirect "/" else session[:errors] = "No such user or bad password." redirect "/login" end end </code></pre> <h1>user.rb</h1> <pre><code>require 'data_mapper' require 'dm-validations' require 'bcrypt' module Kimsin DataMapper.setup :default, 'sqlite3:///home/barerd/RProjects/kimsin/users.db' class User attr_accessor :password, :confirm_password include DataMapper::Resource property :id, Serial property :email, String, :required =&gt; true, :unique =&gt; true, :format =&gt; :email_address, :messages =&gt; { :presence =&gt; "We need your email address.", :is_unique =&gt; "We already have that email.", :format =&gt; "Doesn't look like an email adress.."} property :password_salt, String property :password_hash, String, :length =&gt; 80 validates_presence_of :password, :confirm_password, :messages =&gt; { :presence =&gt; "You have to type a password and confirm it." } validates_format_of :password, :confirm_password, :with =&gt; /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])([\x20-\x7E]){8,40}$/, :messages =&gt; { :format =&gt; "The password should be 8 to 40 characters long and contain at least one digit, one lowercase and one uppercase letter and one special character." } before :save, :encrypt_password def self.authenticate email, password user = User.all :email.like =&gt; email if user &amp;&amp; user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) user else nil end end def encrypt_password if password != nil self.password_salt = BCrypt::Engine.generate_salt self.password_hash = BCrypt::Engine.hash_secret password, password_salt end end end DataMapper.finalize end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload