Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to sign up with json devise

I have a problem with Devise registration using /users/sign_up. I have to pass the registration details via rails terminal using curl like this:

curl: //your-domain:3000/users/sign_up --data "[email protected]&password=123456789&password_confirmation=123456789"

and it should return:

{"success":true,"info":"Registered","email":"[email protected]","auth_token":"N8N5MPqFNdDz3G1jRsC9"}

But I am getting below error message:

ActionController::RoutingError (No route matches [POST] "/users/sign_up"):

While users/sign_in is working perfectly:

curl: //your-domain:3000/users/sign_in.json --data "[email protected]&password=123456789"

return:

{"success":true,"info":"Logged in","auth_token":"czzBpiNS7mkkQJAbHyMo","email":"[email protected]"}

Below is my RegistrationsController class:

class RegistrationsController < Devise::RegistrationsController

before_filter :authenticate_user!, :except => [:create], :if => Proc.new { |c| c.request.format == 'application/json' }
  respond_to :json

  prepend_view_path 'app/views/devise'

  def create
    build_resource
    if resource.save
      sign_in resource
      resource.ensure_authentication_token!
      render :status => 200,
             :json => { :success => true,
                        :info => "Registered",
                        :email => resource.email,
                        :auth_token => resource.authentication_token }
    else
      render :status => :unprocessable_entity,
             :json => { :`enter code here`success => false,
                        :info => resource.errors }
    end
  end

end

and routes.rb: has following settings:

 devise_for(:users, :controllers => { :sessions => "sessions", :registrations => 'registrations' })
like image 377
user3387358 Avatar asked Jan 23 '26 01:01

user3387358


1 Answers

Here's what we use (check it at http://firststopcosmeticshop.co.uk -- "register" @ top)

 class RegistrationsController < DeviseController
  prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
  prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]

  before_filter :configure_permitted_parameters

  prepend_view_path 'app/views/devise'

  # GET /resource/sign_up
  def new
    build_resource({})
    respond_with self.resource
  end

  # POST /resource
  def create
    build_resource(sign_up_params)

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource

      respond_to do |format|
        format.json { render :json => resource.errors, :status => :unprocessable_entity }
        format.html { respond_with resource }
      end
    end
  end
  ....

Paths

I think your error is from your devise paths (trying to send a POST request to /sign_up). You can see the method should be create here:

sign_up (Devise::RegistrationsController#create) - Permits authentication keys plus password and password_confirmation

Why don't you try this in your config/routes.rb file:

devise_for :users, controllers: { sessions: "sessions", registrations: "registrations"}, path_names: { sign_in: 'sign_in', password: 'forgot', confirmation: 'confirm', unlock: 'unblock', sign_up: 'sign_up', sign_out: 'logout'}
like image 69
Richard Peck Avatar answered Jan 24 '26 16:01

Richard Peck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!