为什么我得到一个未初始化的常量 Api::SessionsController: 错误?
Why am I getting an uninitialized constant Api::SessionsController: error?
我不断收到此错误消息:
NameError - uninitialized constant Api::SessionsController:
但我已经仔细检查过,我的路由配置看起来是正确的:
Rails.application.routes.draw do
namespace :api, defaults: {format: :json} do
resources :users, only: :create
resource :session, only: [:create, :destroy]
end
root 'static_pages#root'
end
我的控制器也在使用单数 session
:
class Api::SessionController < ApplicationController
def create
@user = User.find_by_credentials(
params[:user][:username],
params[:user][:password]
)
if @user
log_in(@user)
render 'api/users/show'
else
render json: ['Your request failed. Please try again.'], status: 401
end
end
而我的文件夹结构如下:
Rails有很好的官方指南。
会话是 singular resource 不引用 ID。
Because you might want to use the same controller for a singular route (/account
) and a plural route (/accounts/45
), singular resources map to plural controllers. So that, for example, resource :photo
and resources :photos
creates both singular and plural routes that map to the same controller (PhotosController
).
因此您需要将控制器重命名为 Api::SessionsController
。
如果想保持你设置的样子。
post 'session' => 'session#create', as: :session_create
destroy 'session' => 'session#destroy', as: :session_destroy
我不断收到此错误消息:
NameError - uninitialized constant Api::SessionsController:
但我已经仔细检查过,我的路由配置看起来是正确的:
Rails.application.routes.draw do
namespace :api, defaults: {format: :json} do
resources :users, only: :create
resource :session, only: [:create, :destroy]
end
root 'static_pages#root'
end
我的控制器也在使用单数 session
:
class Api::SessionController < ApplicationController
def create
@user = User.find_by_credentials(
params[:user][:username],
params[:user][:password]
)
if @user
log_in(@user)
render 'api/users/show'
else
render json: ['Your request failed. Please try again.'], status: 401
end
end
而我的文件夹结构如下:
Rails有很好的官方指南。
会话是 singular resource 不引用 ID。
Because you might want to use the same controller for a singular route (
/account
) and a plural route (/accounts/45
), singular resources map to plural controllers. So that, for example,resource :photo
andresources :photos
creates both singular and plural routes that map to the same controller (PhotosController
).
因此您需要将控制器重命名为 Api::SessionsController
。
如果想保持你设置的样子。
post 'session' => 'session#create', as: :session_create
destroy 'session' => 'session#destroy', as: :session_destroy