未初始化的常量SessionsController的API [英] uninitialized constant SessionsController in API

查看:280
本文介绍了未初始化的常量SessionsController的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的API,并在尝试使用API​​来登录,我得到未初始化的常数SessionsController 错误其次是尖尝试运行耙路线的详细信息上可用的路线。当我尝试去到网址

的http://本地主机:3000 / API / V1 /登录 POST

我的路线表明,路线和行动存在如下所示:

  api_v1_users GET /api/v1/users(.:format)API / V1 /用户#{指数:格式=>中的json}
                                 POST /api/v1/users(.:format)API / V1 /用户#创建{:格式=>中的json}
                 new_api_v1_user GET /api/v1/users/new(.:format)API / V1 /用户#新的{:格式=>中的json}
                edit_api_v1_user GET /api/v1/users/:id/edit(.:format)API / V1 /用户编辑#{:格式=>中的json}
                     api_v1_user GET /api/v1/users/:id(.:format)API / V1 /用户#{显示:格式=>中的json}
                                 PUT /api/v1/users/:id(.:format)API / V1 /用户#{更新:格式=>中的json}
                                 DELETE /api/v1/users/:id(.:format)API / V1 /用户#{破坏:格式=>中的json}
         new_api_v1_user_session GET /api/v1/login(.:format)会话#新的{:格式=>中的json}
             api_v1_user_session POST /api/v1/login(.:format)会话#创建{:格式=>中的json}
     destroy_api_v1_user_session DELETE /api/v1/logout(.:format)会话#{破坏:格式=>中的json}
  api_v1_user_omniauth_authorize GET | POST /auth/:provider(.:format)认证#中继{:供应商= GT; /微博| Facebook的/,:格式=>中的json}
   ?api_v1_user_omniauth_callback GET | POST /auth/:action/callback(.:format)认证#( - 组合:微博|脸谱){:格式=>中的json}
            api_v1_user_password POST /api/v1/password(.:format)API / V1 /密码#创建{:格式=>中的json}
        new_api_v1_user_password GET /api/v1/password/new(.:format)API / V1 /密码#新{:格式=>中的json}
       edit_api_v1_user_password GET /api/v1/password/edit(.:format)API / V1 /密码#编辑{:格式=>中的json}
                                 PUT /api/v1/password(.:format)API / V1 /密码#{更新:格式=>中的json}
 cancel_api_v1_user_registration GET /api/v1/cancel(.:format)注册#取消{:格式=>中的json}
        api_v1_user_registration POST /api/v1(.:format)注册#创建{:格式=>中的json}
    new_api_v1_user_registration GET /api/v1/sign_up(.:format)注册#新的{:格式=>中的json}
   edit_api_v1_user_registration GET /api/v1/edit(.:format)注册#{编辑:格式=>中的json}
                                 PUT /api/v1(.:format)注册#{更新:格式=>中的json}
                                 DELETE /api/v1(.:format)注册#{破坏:格式=>中的json}

当我尝试使用 authentication_token URL参数来访问该帐户,它工作正常。

的http://本地主机:3000 / API / V1 /用户/ 39 user_token = DxwspVz​​YSpsiLosd9xcE

使用,我可以让 PUT GET 请求没有问题。

我的路线是这样的:

 命名空间:API,默认值为:{格式:'JSON'}做
    命名空间:v1的做
      资源:用户
      devise_for:用户:PATH => '',path_names:{sign_in:登录,SIGN_OUT:注销},
                                      控制器:{omniauth_callbacks:认证,注册注册,会议会议}
    结束
  结束

我想这是我的SessionsController一个问题,但无法弄清楚,为什么 PUT 要求将工作,但 POST 不会。这里是 SessionsController

 模块API
  模块V1
    类SessionsController<设计:: SessionsController
            的before_filter:的authenticate_user!除了:[:创建:毁灭]
            的before_filter:ensure_params_exist
            skip_before_filter:verify_authenticity_token          打造高清
            资源= User.find_for_database_authentication(电子邮件:PARAMS [:USER_LOGIN] [:邮箱])
            返回invalid_login_attempt除非资源            如果resource.valid_password(PARAMS [:USER_LOGIN] [:密码])?
                sign_in(用户,资源)
                resource.ensure_authentication_token!
                渲染API / V1 /会话/ new.json.jbuilder',状态:201
                返回
            结束
            invalid_login_attempt
          结束          DEF破坏
            current_user.reset_authentication_token
            渲染JSON:{成功:真正}
          结束          保护          高清ensure_params_exist
            返回除非PARAMS [:USER_LOGIN] .blank?
            渲染JSON:{成功:假消息:缺少USER_LOGIN参数},状态:422
          结束          高清invalid_login_attempt
            渲染API / V1 /会话/ invalid.json.jbuilder',状态:401
          结束
    结束
  结束
结束


解决方案

所以在我的路线文件我有

 命名空间:API,默认值为:{格式:'JSON'}做
    命名空间:v1的做
      资源:用户
      devise_for:用户,路径:'',path_names:{sign_in:登录,SIGN_OUT:注销},
                                      控制器:{omniauth_callbacks:认证,注册注册,会议会议}
    结束
  结束

我删除会议:会话键,一切都很正常。我不会以纪念这是正确的答案,因为我不知道为什么这个固定的问题,或者更具体地说,为什么加入会议:会话是造成摆在首位的错误。希望有人能为未来的读者解释这个问题。

编辑:

我认识很久以后,问题是,我是引用 sessions_controller ,当我想引用 API / V1 / sessoins_controller 没有正规 sessions_controller 我只是使用设计的,但有一个 sessions_controller API / V1 命名空间。所以这就是为什么这个问题发生了什么。


有关为别人谁碰到这个问题就来了以后参考,错误是告诉你你正在寻找不存在的,因为它应该控制器,所以要排除故障,确保你在正确的寻找控制器的地方,并以正确的名称。

I'm writing an API and am trying to login with the API and am getting uninitialized constant SessionsController error followed by the tip to "Try running rake routes for more information on available routes." when I try to go to the URL

http://localhost:3000/api/v1/login as a POST

My routes show that the route and action exist as shown:

                    api_v1_users GET      /api/v1/users(.:format)                                        api/v1/users#index {:format=>"json"}
                                 POST     /api/v1/users(.:format)                                        api/v1/users#create {:format=>"json"}
                 new_api_v1_user GET      /api/v1/users/new(.:format)                                    api/v1/users#new {:format=>"json"}
                edit_api_v1_user GET      /api/v1/users/:id/edit(.:format)                               api/v1/users#edit {:format=>"json"}
                     api_v1_user GET      /api/v1/users/:id(.:format)                                    api/v1/users#show {:format=>"json"}
                                 PUT      /api/v1/users/:id(.:format)                                    api/v1/users#update {:format=>"json"}
                                 DELETE   /api/v1/users/:id(.:format)                                    api/v1/users#destroy {:format=>"json"}
         new_api_v1_user_session GET      /api/v1/login(.:format)                                        sessions#new {:format=>"json"}
             api_v1_user_session POST     /api/v1/login(.:format)                                        sessions#create {:format=>"json"}
     destroy_api_v1_user_session DELETE   /api/v1/logout(.:format)                                       sessions#destroy {:format=>"json"}
  api_v1_user_omniauth_authorize GET|POST /auth/:provider(.:format)                                      authentications#passthru {:provider=>/twitter|facebook/, :format=>"json"}
   api_v1_user_omniauth_callback GET|POST /auth/:action/callback(.:format)                               authentications#(?-mix:twitter|facebook) {:format=>"json"}
            api_v1_user_password POST     /api/v1/password(.:format)                                     api/v1/passwords#create {:format=>"json"}
        new_api_v1_user_password GET      /api/v1/password/new(.:format)                                 api/v1/passwords#new {:format=>"json"}
       edit_api_v1_user_password GET      /api/v1/password/edit(.:format)                                api/v1/passwords#edit {:format=>"json"}
                                 PUT      /api/v1/password(.:format)                                     api/v1/passwords#update {:format=>"json"}
 cancel_api_v1_user_registration GET      /api/v1/cancel(.:format)                                       registrations#cancel {:format=>"json"}
        api_v1_user_registration POST     /api/v1(.:format)                                              registrations#create {:format=>"json"}
    new_api_v1_user_registration GET      /api/v1/sign_up(.:format)                                      registrations#new {:format=>"json"}
   edit_api_v1_user_registration GET      /api/v1/edit(.:format)                                         registrations#edit {:format=>"json"}
                                 PUT      /api/v1(.:format)                                              registrations#update {:format=>"json"}
                                 DELETE   /api/v1(.:format)                                              registrations#destroy {:format=>"json"}

When I try to access the account using the authentication_token url parameter, it works fine.

http://localhost:3000/api/v1/users/39?user_token=DxwspVzYSpsiLosd9xcE

Using that I can make PUT and GET requests no problem.

My routes look like this:

namespace :api, defaults: {format: 'json'} do
    namespace :v1 do
      resources :users
      devise_for :users, :path => '', path_names: {sign_in: "login", sign_out: "logout"},
                                      controllers: {omniauth_callbacks: "authentications", registrations: "registrations", sessions: "sessions"}
    end
  end

I assume it's a problem in my SessionsController, but can't figure out why the PUT request would work, but the POST wouldn't. Here is the SessionsController:

module Api
  module V1
    class SessionsController < Devise::SessionsController
            before_filter :authenticate_user!, except: [:create, :destroy]
            before_filter :ensure_params_exist
            skip_before_filter :verify_authenticity_token

          def create
            resource = User.find_for_database_authentication(email: params[:user_login][:email])
            return invalid_login_attempt unless resource

            if resource.valid_password?(params[:user_login][:password])
                sign_in("user", resource)
                resource.ensure_authentication_token!
                render 'api/v1/sessions/new.json.jbuilder', status: 201
                return
            end
            invalid_login_attempt
          end

          def destroy
            current_user.reset_authentication_token
            render json: {success: true}
          end

          protected

          def ensure_params_exist
            return unless params[:user_login].blank?
            render json: {success: false, message: "missing user_login parameter"}, status: 422
          end

          def invalid_login_attempt
            render 'api/v1/sessions/invalid.json.jbuilder', status: 401
          end
    end
  end
end

解决方案

So in my routes file I had

namespace :api, defaults: {format: 'json'} do
    namespace :v1 do
      resources :users
      devise_for :users, path: '', path_names: {sign_in: "login", sign_out: "logout"},
                                      controllers: {omniauth_callbacks: "authentications", registrations: "registrations", sessions: "sessions"}
    end
  end

I removed sessions: "sessions" and everything works great. I'm not going to mark this as the correct answer because I don't know why this fixed the problem, or more specifically, why adding sessions: "sessions" was causing the error in the first place. Hopefully someone can explain this for future readers.

EDIT:

I realized much later that the problem was that I was referencing the sessions_controller, when I wanted to reference the api/v1/sessoins_controller There is no regular sessions_controller I just use the Devise one, but there is a sessions_controller in the API/V1 namespace. So that is why the problem was happening.


For future reference for anyone else who comes across this problem, the error is telling you the controller you're looking for doesn't exist as it should, so to troubleshoot, make sure you are looking for the controller in the correct place, and with the correct name.

这篇关于未初始化的常量SessionsController的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆