gpt4 book ai didi

ruby-on-rails - API 中未初始化的常量 SessionsController

转载 作者:行者123 更新时间:2023-12-05 08:24:12 26 4
gpt4 key购买 nike

我正在编写一个 API 并尝试使用该 API 登录并收到 uninitialized constant SessionsController 错误,然后是“尝试运行 rake 路由以获取有关可用路由的更多信息”的提示。当我尝试访问 URL 时

http://localhost:3000/api/v1/login 作为 POST

我的路由显示路由和 Action 存在如图所示:

                    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

最佳答案

所以在我的路线文件中我有

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" 首先会导致错误.希望有人能为 future 的读者解释这一点。

编辑:

很久以后我才意识到问题是我引用了 sessions_controller,当我想引用 api/v1/sessoins_controller 没有常规的 sessions_controller 我只是使用 Devise 一个,但是 API/V1 命名空间中有一个 sessions_controller。这就是问题发生的原因。


为了将来遇到此问题的任何其他人引用,该错误告诉您您正在寻找的 Controller 不存在,但它应该存在,因此要进行故障排除,请确保您在正确的位置寻找 Controller 地点,并使用正确的名称。

关于ruby-on-rails - API 中未初始化的常量 SessionsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com