1

私はこれをやろうとしています:

class NotesController < ApplicationController

デフインデックス

@user = User.find_by_authentication_token(params[:token])

@notes = @user.notes

...

しかし、私はエラーが発生しています:

Started POST "/api/login" for 10.0.0.4 at 2013-07-22 13:20:43 +0300
Processing by Api::LoginController#login as JSON
  Parameters: {"user"=>{"password"=>"[FILTERED]", "email"=>"pp@pp.pp"}, "login"=>{"user"=>{"password"=>"[FILTERED]", "email"=>"pp@pp.pp"}}}
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'pp@pp.pp' LIMIT 1
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."authentication_token" = 'GFRWkkLCq3xQx5rTRTFp' LIMIT 1
Completed 200 OK in 119ms (Views: 0.0ms | ActiveRecord: 3.0ms)


Started GET "/notes?token=GFRWkkLCq3xQx5rTRTFp" for 10.0.0.4 at 2013-07-22 13:20:44 +0300
Processing by NotesController#index as JSON
  Parameters: {"token"=>"GFRWkkLCq3xQx5rTRTFp", "note"=>{}}
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."authentication_token" = 'GFRWkkLCq3xQx5rTRTFp' LIMIT 1
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `notes' for nil:NilClass):
  app/controllers/notes_controller.rb:7:in `index'

ご覧のとおり、コード

User.find_by_authentication_token(params[:token])

nil を返し続けます。

4

1 に答える 1

0

解決策: 問題は、メソッドuser.ensure_authentication_tokenの後にユーザーを保存 (コミット) するのを忘れていたことです。

def login
    user = User.find_for_database_authentication(:email => params[:user][:email])
    if user.nil?
      render :json => {:result => 'ERROR'}
    else
      if user.valid_password?(params[:user][:password])
        user.ensure_authentication_token
        user.save
        render :json => {:result => user.authentication_token}
于 2013-07-22T11:01:27.143 に答える