简体   繁体   English

Ruby on rails使用rest API调用和Devise进行用户注册

[英]Ruby on rails User registration using rest API call and Devise

Can anyone guide me how to register a user from mobile device (rest API) in ruby on rails. 任何人都可以指导我如何在ruby on rails上从移动设备(rest API)注册用户。 I'm using Devise with Rails 3.0. 我正在使用Devise with Rails 3.0。

it is giving me this following error 它给了我以下错误

NameError in Devise::CustomRegistrationsController#create Devise中的NameError :: CustomRegistrationsController #create

I've override the functionality of devise registration controller with the following. 我用以下方法覆盖了设计注册控制器的功能。

def create
  respond_to do |format|  
    format.html { 
      super 
    }
    format.json {
      build_resource
      if resource.save
         render :status => 200, :json => resource
      else
        render :json => resource.errors, :status => :unprocessable_entity
      end
    }
  end
 end

this solved the problem and I've added 这解决了问题,我补充说

    skip_before_filter :verify_authenticity_token, :only => :create

to avoid authenticity check. 避免真实性检查。

Wouldn't it be easier to make views for mobile than make an app on android/iOS? 制作移动视图比在Android / iOS上制作应用程序更容易吗? If you need API, then go with POST requests at /users/sign_up (and similar), for example, browse localhost:3000/users/sign_up and change form's action parameter to action="/users.json" , then click submit and you will receive the API's response, for me (on vanilla setup): 如果你需要API,那么请在/ users / sign_up(和类似的)中使用POST请求,例如,浏览localhost:3000 / users / sign_up并将表单的action参数更改为action="/users.json" ,然后单击submit和你将收到API的回复(在vanilla设置上):

{"email":["has already been taken"],"password":["doesn't match confirmation","is too short (minimum is 6 characters)"]}

This way you can debug API (which follows standard conventions) with your browser. 这样您就可以使用浏览器调试API(遵循标准约定)。 Notice that only :format parameter changes on rails routes (you can choose .json or .xml for APIs response) 请注意:仅在:format rails路由上的:format参数更改(您可以为API响应选择.json.xml

POST info sent by my browser: 我的浏览器发送的POST信息:

"utf8=✓&authenticity_token=n5vXMnlzrXefnKQEV4SmVM8cFdHDCUxMYWEBMHp9fDw%3D&user[email]=asd%40fasd.org&user[password]=321&user[password_confirmation]=1233&commit=Sign+up"

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

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