我正在使用 jsonapi-resources gem 构建 Rails 5 JSON API。我发现它使用起来很简单,除非我尝试将它与 Devise 配对。我不断收到 404 错误。
我试图将 aUsers::RegistrationResource与 Devise配对Users::RegistrationsController。这是我的代码。
应用程序/控制器/application_controller.rb
class ApplicationController < ActionController::API
include JSONAPI::ActsAsResourceController
end
应用程序/控制器/用户/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
include JSONAPI::ActsAsResourceController
end
应用程序/资源/用户/registration_resource.rb
class Users::RegistrationResource < JSONAPI::Resource
model_name 'User'
attributes :email, :password, :password_confirmation
end
配置/路由.rb
Prefix Verb URI Pattern Controller#Action
auth_user POST /auth_user(.:format) authentication#authenticate_user
users_registrations GET /users/registrations(.:format) users/registrations#index
POST /users/registrations(.:format) users/registrations#create
users_registration GET /users/registrations/:id(.:format) users/registrations#show
PATCH /users/registrations/:id(.:format) users/registrations#update
PUT /users/registrations/:id(.:format) users/registrations#update
DELETE /users/registrations/:id(.:format) users/registrations#destroy
耙路线
users_registrations POST /users/registrations(.:format) users/registrations#create
auth_user POST /auth_user(.:format) authentication#authenticate_user
GET /users/registrations(.:format) users/registrations#index
POST /users/registrations(.:format) users/registrations#create
users_registration GET /users/registrations/:id(.:format) users/registrations#show
PATCH /users/registrations/:id(.:format) users/registrations#update
PUT /users/registrations/:id(.:format) users/registrations#update
DELETE /users/registrations/:id(.:format) users/registrations#destroy
root GET / pages#index
导轨服务器
Started POST "/users/registrations" for ::1 at 2016-10-14 18:08:21 +0100
Processing by Users::RegistrationsController#create as */*
Parameters: {"data"=>{"type"=>"users", "attributes"=>{"email"=>"neil@examples.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}
[Devise] Could not find devise mapping for path "/users/registrations".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
@request.env["devise.mapping"] = Devise.mappings[:user]
Completed 404 Not Found in 0ms (ActiveRecord: 0.0ms)
AbstractController::ActionNotFound (Could not find devise mapping for path "/users/registrations".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
@request.env["devise.mapping"] = Devise.mappings[:user]
):
这是我与 Postman 一起发布的数据:
POST http://localhost:3000/users/registrations
Content-Type application/vnd.api+json
{
"data": {
"type": "users",
"attributes": {
"email": "neil@examples.com",
"password": "Whatevs",
"password_confirmation": "Whatevs"
}
}
}
我猜首先这是路线的问题。任何人都可以帮忙吗?