2017-08-14 9 views
0

のために未定義のローカル変数やメソッド `電子メールを」私は私のRails APIアプリにJWTを実装したいです。前に直接私がthisPluralSightブログを使用してダミーのプロジェクトを実施しようとしていた私の主なプロジェクトを変更しようとしています。はどのように私は次のエラー解決することができます#<のauthenticateUser>

私はそのチュートリアル/ブログで言及したすべての手順に従いました。しかし、このエラーが表示され続ける:

"status": 500, 
"error": "Internal Server Error", 
"exception": "#<NameError: undefined local variable or method `email' for #<AuthenticateUser:0x684ca38>>", 
"traces": { 
    "Application Trace": [ 
     { 
      "id": 0, 
      "trace": "app/commands/authenticate_user.rb:16:in `user'" 
     }, 
     { 
      "id": 1, 
      "trace": "app/commands/authenticate_user.rb:10:in `call'" 
     }, 
     { 
      "id": 3, 
      "trace": "app/controllers/authentication_controller.rb:7:in `authenticate'" 
     } 

誰でもこのエラーをよく理解するのを助けてくれますか?なぜこれが起こっているように?これをどのようにデバッグするのですか?
これは非常にあなたのソリューションを説明してくださいレールを使用して、私の第二週で&ギミエラーのこの種類を解決する方法についていくつかのインサイダーアプローチ。

App/commands/AuthenticateUserクラス:

class AuthenticateUser 
    prepend SimpleCommand 

    def initialize(email, password) 
    @email = email 
    @password = password 
    end 

    def call 
    JsonWebToken.encode(user_id: user.id) if user 
    end 

    private 

    def user 
    user = User.find_by_email(email) 
    return user if user && user.authenticate(password) 

    errors.add :user_authentication, 'invalid credentials' 
    nil 
    end 
end 

App/controllers/authentication_controller.rbクラス:

class AuthenticationController < ApplicationController 

    skip_before_action :authenticate_request 

    def authenticate 
    au=AuthenticateUser.new(params[:email], params[:password]) 
    command = au.call() 
    if command.success? 
     render json: { auth_token: command.result } 
    else 
     render json: { error: command.errors }, status: :unauthorized 
    end 
    end 
end 

スキーマ:

ActiveRecord::Schema.define(version: 20170814062006) do 

    create_table "items", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t| 
t.string "name" 
t.text "description" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
    end 

    create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t| 
     t.string "name" 
     t.string "email" 
     t.string "password_digest" 
     t.datetime "created_at", null: false 
     t.datetime "updated_at", null: false 
     end 

    end 
そのブログを1として

答えて

0

<NameError: undefined local variable or method `email' for #<AuthenticateUser:0x684ca38>>"

、あなたが不足しているattr_accessor :email, :passwordで、そのエラーが発生しました。あなたがattr_accessorを逃しAuthenticateUser

class AuthenticateUser 
    prepend SimpleCommand 

    def initialize(email, password) 
    @email = email 
    @password = password 
    end 

    def call 
    JsonWebToken.encode(user_id: user.id) if user 
    end 

    private 

    attr_accessor :email, :password #here 

    def user 
    user = User.find_by_email(email) 
    return user if user && user.authenticate(password) 

    errors.add :user_authentication, 'invalid credentials' 
    nil 
    end 
end 
+0

どうすればいいですか? –

+0

@irdalin http://ruby-doc.org/core-2.0.0/Module.html#method-i-attr_accessorをご覧ください – Pavan

0

でそれを追加してみてください。名前が示すように、オブジェクトの属性へのアクセスに役立ちます。それはgetters & settersまたはreader & writerの必要性を排除します。

関連する問題