我最近开始使用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, 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,\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", "password": "password", "password_confirmation": "password" } }更多推荐
通过API在Devise中进行用户注册
发布评论