通过 API 在 Devise 中注册用户 [英] User Registration in Devise through API

查看:57
本文介绍了通过 API 在 Devise 中注册用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 rails 5 开发 REST API.我正在尝试通过 API 调用执行用户粗略的操作.我已经为用户设置了一个基本的设计.

I have recently started developing REST APIs with rails 5. I am trying to do User crude actions through API calls. I have set up a basic devise for users.

在我的路线中,我有这个:

In my routes I have this:

Rails.application.routes.draw do
  devise_for :users, :controllers => {sessions: 'sessions', registrations: 'registrations'}
end

我创建了两个单独的控制器 session_controller 和 registrations_controller 来覆盖设计方法并使其接受 JSON 格式.

I created two separate controllers sessions_controller and registrations_controller to override devise methods and make it accept JSON format.

class RegistrationsController < Devise::RegistrationsController
    respond_to :json
end

class SessionsController < Devise::SessionsController
    respond_to :json
end

我正在尝试注册传递以下数据的用户:

I am trying to register a user passing in the following data:

{  
    user: {
        email: me@gmail.com,
        password: password,
        password_confirmation: password
    }
}

我收到状态 200.但在我的 Rails 服务器中,我收到以下错误:

I get a status 200. but in my rails server I'm getting the following error:

Started POST "/users" for 23.233.9.94 at 2016-12-22 08:22:11 +0000
Processing by RegistrationsController#create as */*
  Parameters: {"Payload: {  \r\n    user: {\r\n        email: me@gmail.com,\r\n        password: password,\r\n        password_confirmation: password\r\n    }\r\n}"=>"[FILTERED]"}
   (0.2ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 200 OK in 5ms (Views: 0.2ms | ActiveRecord: 0.3ms)

所以我基本上有两个问题.如何格式化 JSON,以便我的设计控制器识别它并将其保存到数据库中.我的第二个问题是如何传入 CSRF 令牌.一个小例子真的可以帮助我在这里.提前致谢.

So I basically have 2 questions. How do I format the JSON so that my devise controllers recognize it and save it to the database. And my second question is how do I pass in a CSRF token. A little example would really help me out here. Thanks in advance.

推荐答案

您的 JSON 无效.您需要用双引号将字符串括起来

Your JSON is not valid.You need to enclose the string with double quotes

{  
    "user": {
        "email": "me@gmail.com",
        "password": "password",
        "password_confirmation": "password"
    }
}

这篇关于通过 API 在 Devise 中注册用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆