title: Cannot get BCrypt autentication to work
body: <p>I am trying to set up a simple iOS/Rails application where I can create a user and subsequently authenticate them using BCrypt. I have tried to follow BCrypt's instructions but my authentication process is not working. Very new to rails so I'm sure I'm screwing up something simple!</p>
  
  <p>To create a new user:</p>
  
  <p>In users_controller.rb</p>
  
  <pre><code># POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    @user.password = user_params[:password]
  
   respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
   end
  end
  
  def user_params
      params.require(:user).permit(:username, :password)
  end
  </code></pre>
  
  <p>In user.rb</p>
  
  <pre><code>def password
     @password ||= Password.new(password_hash)
  end
  
  def password=(new_password)
     @password = Password.create(new_password)
     self.password_hash = @password
  end
  </code></pre>
  
  <p>On the server:</p>
  
  <p><em>Started POST "/users.json" for 192.168.2.6 at 2014-08-21 19:32:05 -0500
  Processing by UsersController#create as JSON
    Parameters: {"username"=>"testtest", "password"=>"[FILTERED]", "user"=>{"username"=>"testtest"}}
     (0.1ms)  begin transaction
    User Exists (20.8ms)  SELECT  1 AS one FROM "users"  WHERE "users"."username" = 'testtest' LIMIT 1
  Binary data inserted for <code>string</code> type on column <code>password_hash</code>
    SQL (0.8ms)  INSERT INTO "users" ("created_at", "password_hash", "updated_at", "username") VALUES (?, ?, ?, ?)  [["created_at", "2014-08-22 00:32:05.960378"], ["password_hash", "$2a$10$UzLdD7ytQXKRDU5MhAawJuFQ3R4oQlPyXh/V4GehNP692fsSpK6wK"], ["updated_at", "2014-08-22 00:32:05.960378"], ["username", "testtest"]]
     (1.3ms)  commit transaction
    Rendered users/show.json.jbuilder (0.3ms)
  Completed 201 Created in 136ms (Views: 31.9ms | ActiveRecord: 23.5ms)</em></p>
  
  <p>The authenticate a user:</p>
  
  <p>In sessions_controller.rb</p>
  
  <pre><code>def create
      if @user = User.authenticate(params[:username], params[:password])
          session[:user_id] = @user.id
          render :show
      else
          flash.now[:alert] = "Invalid login/password combination"
          render :action =&gt; 'fail'
      end
  end
  </code></pre>
  
  <p>In user.rb</p>
  
  <pre><code>def self.authenticate(username,password)
   @user = User.find_by_username(username)
   return @user.password == password
  end
  </code></pre>
  
  <p>Server:</p>
  
  <p>*Started POST "/session" for 192.168.2.6 at 2014-08-21 19:38:26 -0500
  Processing by SessionsController#create as <em>/</em>
    Parameters: {"username"=>"testtest", "password"=>"[FILTERED]", "session"=>{"username"=>"testtest", "password"=>"[FILTERED]"}}
    User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."username" = 'testtest' LIMIT 1
    Rendered sessions/fail.json.jbuilder (0.2ms)
  Completed 200 OK in 105ms (Views: 23.3ms | ActiveRecord: 0.2ms)*</p>
  
  <p>Thanks much for any insight!</p>