gpt4 book ai didi

ruby-on-rails - 无法使用 json devise 注册

转载 作者:行者123 更新时间:2023-12-04 02:02:26 24 4
gpt4 key购买 nike

我在使用/users/sign_up 进行 Devise 注册时遇到问题。我必须像这样使用 curl 通过 rails 终端传递注册详细信息:

curl: //your-domain:3000/users/sign_up --data "email=jump@gmail.com&password=123456789&password_confirmation=123456789"

它应该返回:

{"success":true,"info":"Registered","email":"jump@example.com","auth_token":"N8N5MPqFNdDz3G1jRsC9"}

但我收到以下错误消息:

ActionController::RoutingError (No route matches [POST] "/users/sign_up"):

当 users/sign_in 完美运行时:

curl: //your-domain:3000/users/sign_in.json --data "email=abc@gmail.com&password=123456789"

返回:

{"success":true,"info":"Logged in","auth_token":"czzBpiNS7mkkQJAbHyMo","email":"abc@gmail.com"}

下面是我的 RegistrationsController 类:

class RegistrationsController < Devise::RegistrationsController

before_filter :authenticate_user!, :except => [:create], :if => Proc.new { |c| c.request.format == 'application/json' }
respond_to :json

prepend_view_path 'app/views/devise'

def create
build_resource
if resource.save
sign_in resource
resource.ensure_authentication_token!
render :status => 200,
:json => { :success => true,
:info => "Registered",
:email => resource.email,
:auth_token => resource.authentication_token }
else
render :status => :unprocessable_entity,
:json => { :`enter code here`success => false,
:info => resource.errors }
end
end

end

和 routes.rb: 具有以下设置:

 devise_for(:users, :controllers => { :sessions => "sessions", :registrations => 'registrations' })

最佳答案

这是我们使用的内容(在 http://firststopcosmeticshop.co.uk 中查看——“注册”@top)

 class RegistrationsController < DeviseController
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]

before_filter :configure_permitted_parameters

prepend_view_path 'app/views/devise'

# GET /resource/sign_up
def new
build_resource({})
respond_with self.resource
end

# POST /resource
def create
build_resource(sign_up_params)

if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource

respond_to do |format|
format.json { render :json => resource.errors, :status => :unprocessable_entity }
format.html { respond_with resource }
end
end
end
....

路径

我认为您的错误来自于您的设计路径(尝试向 /sign_up 发送 POST 请求)。可以看到the method should be create here :

sign_up (Devise::RegistrationsController#create) - Permits authentication keys plus password and password_confirmation

你为什么不在你的 config/routes.rb 文件中试试这个:

devise_for :users, controllers: { sessions: "sessions", registrations: "registrations"}, path_names: { sign_in: 'sign_in', password: 'forgot', confirmation: 'confirm', unlock: 'unblock', sign_up: 'sign_up', sign_out: 'logout'}

关于ruby-on-rails - 无法使用 json devise 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22220339/

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