Mahmoud Sultan
Mahmoud Sultan

Reputation: 570

No route matches [POST] "/users"

I'm new to rails, and I'm trying to build API following Code School tutorial. I get this error while trying to post to '/users' path.

routes file code :

Rails.application.routes.draw do
  namespace :api,constraints: {subdomain: 'api'}, path: '/' do
   resources :users, except: :delete
  end
end

and the test code is :

require 'test_helper'
class CreatingUsersTest < ActionDispatch::IntegrationTest
  test 'create users' do
   post api_users_path,
      {user: {name: 'test', email:'[email protected]'}}.to_json,
      {'Accept' => Mime::JSON, 'Content-Type': Mime::JSON.to_s}
   assert_equal response.status, 201
   assert_equal response.content_type, Mime::JSON
   user = json(response.body)
   assert_equal api_user_url(user[:id]), response.location
 end
end

And when I use rake routes :

    api_users GET /users(.:format) api/users#index {:subdomain=>"api"}
              POST   /users(.:format) api/users#create {:subdomain=>"api"}
    ....

Upvotes: 0

Views: 152

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176372

In the route you constraint the subdomain to be api

namespace :api,constraints: {subdomain: 'api'}, path: '/' do

but then in the test you call api_user_path

post api_users_path

that uses a generic hostname (test.host), and not the api hostname. The solution is to pass a specific host to the helper that satisfies the requirement.

post api_users_path(host: "api.test.host")

Upvotes: 0

Related Questions