json のみに応答する単純な登録モデルを作成しようとしています。
class Api::RegistrationController < ApplicationController
skip_before_filter :verify_authenticity_token
respond_to :json
def register
build_resource(sign_up_params)
if resource.save
render :status => "OK"
else
render :status => "ERROR"
end
end
end
登録しようとすると、次のようになります。
Started POST "/api/register" for 10.0.0.4 at 2013-07-22 07:45:44 +0300
Processing by Api::RegistrationController#register as JSON
Parameters: {"user"=>{"password_confirmation"=>"[FILTERED]", "password"=>"[FILTERED]", "email"=>"mmm@mmm.mmm"}, "registration"=>{"user"=>{"password_confirmation"=>"[FILTERED]", "password"=>"[FILTERED]", "email"=>"mmm@mmm.mmm"}}}
Completed 500 Internal Server Error in 1ms
NameError (undefined local variable or method `sign_up_params' for #<Api::RegistrationController:0x4eca9e0>):
app/controllers/api/registration_controller.rb:5:in `register'
Deviseに属する sign_up_paramsを使用しようとしているので、これは明らかです。
次に、継承を試みます。
class Api::RegistrationController < Devise::RegistrationController
サーバーを再起動すると、次のエラーが発生します。
Started POST "/api/register" for 10.0.0.4 at 2013-07-22 07:50:03 +0300
ActionController::RoutingError (uninitialized constant Devise::RegistrationController):
app/controllers/api/registration_controller.rb:1:in `<top (required)>'
これは私のルートです:
Notter12::Application.routes.draw do
post "api/register" => "api/registration#register"
devise_for :users
.
.
.
レーキ:
api_register POST /api/register(.:format) api/registration#register
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
notes GET /notes(.:format) notes#index
POST /notes(.:format) notes#create
new_note GET /notes/new(.:format) notes#new
edit_note GET /notes/:id/edit(.:format) notes#edit
note GET /notes/:id(.:format) notes#show
PUT /notes/:id(.:format) notes#update
DELETE /notes/:id(.:format) notes#destroy
root / notes#index
Process finished with exit code 0
編集:解決策
class Api::RegistrationController < Devise::RegistrationsController
skip_before_filter :verify_authenticity_token
respond_to :json
def register
build_resource(sign_up_params)
if resource.save
render :json => {:result => 'OK'}
else
render :json => {:result => 'ERROR'}
end
end
end
また、routes.rb を変更する必要がありました。
devise_scope :user do
post "api/register" => "api/registration#register"
end
devise_for :users
.
.
.