2013-09-27 14 views
0

我正在编写一个API,并试图使用API​​登录,并得到uninitialized constant SessionsController错误,随后提示“尝试运行耙路由以获取有关可用路由的更多信息。”。当我尝试去URL未初始化的常量SessionsController在API中

http://localhost:3000/api/v1/loginPOST

我的路线显示,路线和行动存在如图所示:

    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"} 

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

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

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

我的路线是这样的:

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 

我以为这是我的SessionsController一个问题,但无法弄清楚,为什么PUT要求将工作,但POST不会。这里是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 

回答

8

所以在我的路线文件我有

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 

我删除sessions: "sessions",一切的伟大工程。我不打算将此标记为正确的答案,因为我不知道为什么这会解决问题,或者更具体地说,为什么首先添加sessions: "sessions"会导致错误。希望有人能为未来的读者解释这一点。

编辑:

我认识很久以后,问题是,我是引用sessions_controller,当我想引用api/v1/sessoins_controller没有正规sessions_controller我只是用设计之一,但在sessions_controller命名空间为API/V1。所以这就是问题发生的原因。


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