1

サンプル ログイン プログラムには、2 つのクラスがあります。セッションとユーザー。user テーブルには、password という名前の 2 つのフィールドがあります。セッション メソッドは、ユーザー クラスの認証メソッドを呼び出すときに「NoMethodError」を取得し続けます。

ユーザークラスは次のようになります。

class UserController < ApplicationController
  def index
    @user = User.find(:first, :order => 'RANDOM()')
  end

  def show
   @user = User.find(:all)
  end

  def new
    @user = User.new
   #redirect_to user_path
  end

  def create
    @user = User.new(params[:user])
    @user.save
    #redirect_to 'http://localhost:3000/user/show'
  end


  def self.authenticate( password)
    #user = User.find_by_name(name)

    if user.find_by_password(password)#match_password(password)
      return true
    else
      return false
    end
  end

 def self.match_password(password="")
   encrypted_password == BCrypt::Engine.hash_secret(password, salt)
 end  
end

セッション クラスには、ユーザーの認証 fn を呼び出す create メソッドがありました。これが作成方法です

def create
   user = User.find_by_name(params[:session][:name].downcase)

   if user && user.authenticate(params[:session][:password])
     # Sign the user in and redirect to the user's show page.
     sign_in user
     redirect_to user
   else
     flash.now[:error] = 'Invalid email/password combination' # Not quite right!
     render 'new'
   end
end

ここにスタックがあります

Started POST "/sessions" for 127.0.0.1 at Sun Sep 30 15:21:02 -0400 2012
Processing by SessionsController#create as HTML
  Parameters: {"commit"=>"Sign in", "session"=>{"name"=>"ar", "password"=>"[FILTERED]"}, "authenticity_token"=>"vxYHw/4JV2fJuvvBN4GVLv31aBl8ETEtQPBB/483q/Q=", "utf8"=>"✓"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."name" = 'ar' LIMIT 1
Completed 500 Internal Server Error in 4ms

NoMethodError (undefined method `authenticate' for #<User:0xb69650e4>):
  app/controllers/sessions_controller.rb:10:in `create'

よろしくお願いします、A.

4

1 に答える 1