Rails GrapeAPI + Authlogic未定义方法`session'

时间:2017-02-01 22:11:04

标签: ruby-on-rails ruby authlogic grape-api

我正在尝试使用GrapeAPIAuthlogic构建用于登录的API。

我的代码如下所示:

class Api
  class V1
    class UserSessionsController < Grape::API


    post :login do
      @user_session = UserSession.new(params[:user_session])
      if @user_session.save
        fetch_api_key(@user_session.user)
      else
        error!('Unauthorized.', 401)
      end
    end

    helpers do
      def current_user_session
        return @current_user_session if defined?(@current_user_session)
        @current_user_session = UserSession.find
      end

      def current_user
        return @current_user if defined?(@current_user)
        @current_user = current_user_session && current_user_session.user
       end

      end

    end
  end
end

问题在于,当我运行@user_session = UserSession.new(params[:user_session])时,我得到You must activate the Authlogic::Session::Base.controller with a controller object before creating objects

我尝试添加

Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)

在尝试创建UserSession之前,现在我在运行undefined method 'session' for #<#<Class:0x007fa7a63ade28>:0x007fa7a7b144b8>时获得@user_session.save

PS:我还尝试将authenticate_with User添加到user_session.rb文件,然后

acts_as_authentic do |c| c.session_class = UserSession end

user模型中,但它不起作用。

PS2:我使用的是rails 4,2,7,grape 0.18.0和authlogic 3.4.6

0 个答案:

没有答案