简体   繁体   English

通过API在Devise中进行用户注册

[英]User Registration in Devise through API

I have recently started developing REST APIs with rails 5. I am trying to do User crude actions through API calls. 我最近开始使用Rails 5开发REST API。我正在尝试通过API调用来执行用户粗略操作。 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

I created two separate controllers sessions_controller and registrations_controller to override devise methods and make it accept JSON format. 我创建了两个单独的控制器sessions_controller和registrations_controller来覆盖devise方法并使其接受JSON格式。

class RegistrationsController < Devise::RegistrationsController
    respond_to :json
end

and

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
    }
}

I get a status 200. but in my rails server I'm getting the following error: 我的状态为200。但是在我的Rails服务器中,出现以下错误:

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)

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. 如何格式化JSON,以便我的设计控制器识别它并将其保存到数据库。 And my second question is how do I pass in a CSRF token. 我的第二个问题是如何传递CSRF令牌。 A little example would really help me out here. 一个小例子确实可以帮助我。 Thanks in advance. 提前致谢。

Your JSON is not valid.You need to enclose the string with double quotes 您的JSON无效。您需要将字符串用双引号引起来

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM