-6ren">
gpt4 book ai didi

ruby-on-rails - Rails RoutingError(未初始化的常量

转载 作者:行者123 更新时间:2023-12-04 04:48:39 25 4
gpt4 key购买 nike

我正在尝试创建一个仅响应 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'

这很明显,因为我正在尝试使用 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
.
.
.

rake :
            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
.
.
.

最佳答案

请重命名为
Devise::RegistrationsController不是 注册 应该是 注册

谢谢希望它会起作用

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

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