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

查看:111
本文介绍了通过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来覆盖devise方法并使其接受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)

所以我基本上有2个问题. 如何格式化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天全站免登陆