Reputation: 45
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' })
Upvotes: 3
Views: 1166
Reputation: 2139
If we run rake routes
we can see that devise sign up (registration) has [new, create, update, ..]
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
So, the route #new
shows only the sign up page and to register new user we use #create
and it's correct route: /users.json
not /user/sign_up.json
As we can see here:
POST /users(.:format) devise/registrations#create
Here's sign_up page inspect on browser, we can see action = "/users"
Upvotes: 1
Reputation: 1548
try these code ,I do it with my rails api
devise_scope :user do
post 'users' => 'registrations#create', :as => 'user_registration'
end
curl: //your-domain:3000/users --data "[email protected]&password=123456789&password_confirmation=123456789"
Upvotes: 0
Reputation: 76774
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'}
Upvotes: 1