问题描述
限时送ChatGPT账号..我正在编写 API 并尝试使用该 API 登录,但遇到 uninitialized constant SessionsController
错误,然后是尝试运行 rake 路由以获取有关可用路由的更多信息"的提示.当我尝试访问 URL
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
作为 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"}
当我尝试使用 authentication_token
url 参数访问帐户时,它工作正常.
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
使用它我可以使 PUT
和 GET
请求没问题.
Using that I can make PUT
and GET
requests no problem.
我的路线如下所示:
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
:
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
我删除了 sessions: "sessions"
并且一切正常.我不会将其标记为正确答案,因为我不知道为什么这可以解决问题,或者更具体地说,为什么添加 sessions: "sessions"
是首先导致错误的原因.希望有人可以为未来的读者解释这一点.
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.
后来我意识到问题是我引用了sessions_controller
,当我想引用api/v1/sessoins_controller
没有常规的session_controller
我只使用了 Devise,但在 API/V1
命名空间中有一个 sessions_controller
.所以这就是问题发生的原因.
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.
这篇关于API 中未初始化的常量 SessionsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论