Ruby on rails User registration using rest API call and Devise

谁说胖子不能爱 提交于 2019-12-02 21:24:54

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? 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):

{"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. Notice that only :format parameter changes on rails routes (you can choose .json or .xml for APIs response)

POST info sent by my browser:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!