No route [GET] / user matches

Edit: The resource and controller have been pluralized thanks to the feedback written in the comments. But my problem still persists.

I am very new to Rails and I am trying to create a REST API in Rails 4. I have a routing error when I try to make a GET request to a User

ressource:http://api.localhost:3000/users

No route match [GET] "/ users"

But when I execute the "rake routes" command in my terminal, I see that there is a route /users

:

    api_users GET    /users(.:format)          api/users#index {:subdomain=>"api"}
              POST   /users(.:format)          api/users#create {:subdomain=>"api"}  
 new_api_user GET    /users/new(.:format)      api/users#new {:subdomain=>"api"}
edit_api_user GET    /users/:id/edit(.:format) api/users#edit {:subdomain=>"api"}
     api_user GET    /users/:id(.:format)      api/users#show {:subdomain=>"api"}
              PATCH  /users/:id(.:format)      api/users#update {:subdomain=>"api"}
              PUT    /users/:id(.:format)      api/users#update {:subdomain=>"api"}
              DELETE /users/:id(.:format)      api/users#destroy {:subdomain=>"api"}

      

Here is the content of my file route.rb

:

Rails.application.routes.draw do

  # create rousources in subdomain api
  namespace :api, path: '/', constraints: { subdomain: 'api' } do
    resources :users
  end

end

      

And the file users_controller.rb

:

module Api
    class UsersController < ApplicationController

        def index
            @users = User.all

            render json: users, status: 200
        end

    end
end

      

Any suggestion?

Edit: The resource and controller have been pluralized thanks to the feedback written in the comments. But my problem still persists.

+3


source to share


3 answers


Rails determines subdomain

URLs by taking that portion from the host address that is before the second dot from the right. In your example, you are using host api.localhost

. Rails will resolve an empty subdomain from this.

So I suggest setting up a domain like api.my_app.dev

that in your hosts

. This would allow Rails to determine the correct portion of the subdomain api

from the host.



After that, your local server can respond with http://api.my_app.dev:3000/users

Btw. I chose the top level domain dev

as it is the default if you are using a tool like POW .

+8


source


you need to configure your hosts:

sudo vim /etc/hosts

add



127.0.0.1 api.test.com

then rails s

and browse " http://api.test.com:3000/user " it will work!

Sorry my Enlish, I hope you can help!

+4


source


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

      

this is not correct you need to check root :to => 'adm001s#index'

+1


source







All Articles