Open luxxi opened 9 years ago
The devise_for :users
line is not inside the api
namespace, if you want to use the devise controllers, you might need to override the response format
@kurenn , have you tried sending actual requests to the app with curl or Postman?
Looking at rake routes
, we can see that there is actually two same routes for POST /users
:
user_registration POST /users(.:format) devise/registrations#create
api_users POST /users(.:format) api/v1/users#create {:format=>:json, :subdomain=>"api"}
So when you run tests, you explicitly call the #create
method of the UsersController
, but when you send a real request, it actually routes to devise/registrations#create
. It looks like like this is the problem @luxxi is having.
If I understood correctly, we still need the Devise routes, since without them the sign_in
method doesn't work. I managed to mitigate it by setting devise_for :users
after the :api
namespace in the routes.rb
file, but it seems to me that there's a larger problem here that needs addressing. Or am I missing something?
@pavel-clarityhk You are totally right, I missed that! sorry about that.
That is actually a good solution, just to first call the :api
namespace and then devise_for :users
I'll be updating the book with these and many other fixes.
Thanks!
@kurenn Also seeing this. Would love to see the updates! I'm building a production backend following your tutorial
class Api::V1::RegistrationsController < Devise::RegistrationsController
respond_to :json
def create
print 'in here'
retailer = Retailer.new(retailers_params)
if retailer.save
render json: retailer, status: 201, location: [:api, retailer]
else
render json: {errors: retailer.errors}, status: 422
end
end
private
def retailers_params
params.require(:retailer).permit(:user_name, :password, :password_confirmation)
end
end
require 'api_constraints'
CaoWuApi::Application.routes.draw do
mount SabisuRails::Engine => "/sabisu_rails"
namespace :api, defaults: {format: :json}, constraints: {subdomain: 'api'}, path: '/' do
scope module: :v1,
constraints: ApiConstraints.new(version: 1, default: true) do
devise_for :retailers, :controller => {:registrations => 'registrations'}
resources :retailers, :only => [:show] do
resources :products, :only => [:create, :index, :show]
end
resources :sessions, :only => [:create, :destroy]
end
end
# https://github.com/kurenn/market_place_api/issues/8
end
btw: Retailer = User in this context
@luxxi were you able to solve it?
@kurenn Moved devise_for :users
next to api definition solves my issue, I just want to understand why, or any simple explanation, can you?
What do you mean by next to api definition
?
I mean, in the routes, after the api namespace definition, that what I wanted to say...
Oh yeah, it is because the /users
path should be handle by the api and not by devise. The routes
file matches the first route when the server is asked to serve.
So in this case you first serve with the API instead of the devise generated routes.
I was having a similar problem to this and solved it by skipping the devise registrations route.
devise_for :users, skip: :registrations
I finished Chapter 3. GET, PATCH and DELETE are working, but on POST request I get
ActionController::UnknownFormat (ActionController::UnknownFormat)