Ruby on rails User registration using rest API cal

2019-03-11 12:51发布

问题:

Can anyone guide me how to register a user from mobile device (rest API) in ruby on rails. I'm using Devise with Rails 3.0.

it is giving me this following error

NameError in Devise::CustomRegistrationsController#create

回答1:

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.



回答2:

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"