使用rails 5试图创建一个简单的api来注册用户。
控制器:
class Api::V1::SessionsController < Devise::SessionsController
protect_from_forgery
respond_to :json
skip_before_action :verify_authenticity_token #, only: [:create]
api :POST, "/v1/create_account", "Create User Account"
param :user, Hash do
param :email, :undef
param :password, :undef
end
def create_account
user = params[:user]
existing_user = User.find_by_email user[:email].strip
return email_already_taken if !existing_user.nil?
user = User.create(:email => user[:email],
:password => user[:password],
:password_confirmation => user[:password])
end
end
路线:
Rails.application.routes.draw do
devise_for :admin_users
namespace :api, defaults: {format: "json"} do
namespace :v1 do
devise_for :users, :controllers => {:sessions => "api/v1/sessions"}, :path_prefix => 'api/v1'
devise_scope :user do
post :create_account, to: "sessions#create_account"
end
end
end
end
跟踪:
跟踪正在讨论控制器的api:POST部分,但为什么会出现此错误?
ActionController::RoutingError (undefined method `api' for Api::V1::SessionsController:Class):
app/controllers/api/v1/sessions_controller.rb:6:in `<class:SessionsController>'
app/controllers/api/v1/sessions_controller.rb:1:in `<top (required)>'
Rendering /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
Rendering /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.8ms)
Rendered collection of /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb [31 times] (10.4ms)
Rendered /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/routes/_table.html.erb (6.7ms)
Rendering /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (9.7ms)
Rendered /Users/local/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (112.5ms)
source=rack-timeout id=43f3c961f7e1e94e4ffe0a55b167d831 timeout=15000ms service=415ms state=completed
我该如何解决这个问题?