kurenn / market_place_api

The API on Rails tutorial application
142 stars 70 forks source link

ActionController::RoutingError (No route matches [GET] "/api/v1/users/1") #54

Open Orinameh opened 8 years ago

Orinameh commented 8 years ago

Hi, when i try to test my endpoint for the first user, i get the html structure with 404 not found is title. The webrick server shows ....No route matches[GET]"/api/v1/users/1". Any help from your end?

lesreaper commented 8 years ago

I'd be interested in a solution here as well.

Innarticles commented 8 years ago

@Orinameh Are u using prax? If you are not using prax. I suggest you use it. It makes following the book easier. Else you remove "constraints: { subdomain: 'api' }" from the route.rb file.

kurenn commented 8 years ago

@Orinameh did you made it work?

Orinameh commented 8 years ago

@Innarticles I use prax while following the tutorial and still get same errors..@Kurenn nope

kurenn commented 8 years ago

@Orinameh How does your routes file looks like?

oseifrimpong commented 8 years ago

@kurenn Hey, I am also having this problem! Below is my routes file.

    require 'api_constraints'
Rails.application.routes.draw` do

  devise_for :users
  #API Definition
    namespace :api, defaults: { format: :json}, 
                constraints: { subdomain: 'api' }, path: '/'  do

        scope module: :v1,
                    constraints: ApiConstraints.new(version: 1, default: true) do

        resources :users, :only => [:show]
    end
  end
end
`
kurenn commented 8 years ago

Try to move the devise_for :users call below the namespace for the API

oseifrimpong commented 8 years ago

@kurenn I am still having the same problem. I have no idea why. I have done what you asked me to do.

Innarticles commented 8 years ago

@Orinameh @lesreaper This is what worked for me:

require 'api_constraints'

MarketPlaceApi::Application.routes.draw do
  resources :articles
  mount SabisuRails::Engine => "/sabisu_rails"

  # devise_for :users
  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do
    # devise_for :users
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      # devise_for :users
      resources :users, :only => [:show, :create, :update, :destroy] do
        resources :products, :only => [:create, :update, :destroy]
        resources :orders, :only => [:index, :show, :create]
      end
      devise_for :users
      resources :sessions, :only => [:create, :destroy]
      resources :products, :only => [:show, :index]
    end
  end

  resources :users, :only => [:show, :new]

end

Also note that the tutorial used an older version of rails.

oseifrimpong commented 8 years ago

@kurenn I am trying to test the user endpoints before the before I install Sabisu. I have tried bringing the devise_for :users below the namespace for the API but I seem to get the same error. which is ActionController::RoutingError (No route matches [GET] "/api/v1/users/1") Any other thing you think might be wrong?

This is my routes.rb once again after doing what you asked me to do.

`require 'api_constraints'

MsosiApi::Application.routes.draw do

  # devise_for :users
  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do
    # devise_for :users
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      # devise_for :users
      resources :users, :only => [:show, :create, :update, :destroy] 

      end
    end
     devise_for :users
end`
kurenn commented 8 years ago

I see, you have to update your routes.rb file.

The thing with that is that you are trying to run the api through a subdomain a accessing this endpoint localhost:3000/api/v1/users/1 and you need to go api.marketplaceapi.dev/users/1 or whichever the endpoint you are trying yo reach.

phani627 commented 7 years ago

To use localhost:3000/api/v1/users/1 change routes.rb to this namespace :api, defaults: { format: :json } do namespace :v1 do devise_for :users resources :users, :only => [:show] end end

AndreiMotinga commented 7 years ago

funny enough I had to change constraints: { subdomain: :api } to constraints: { subdomain: 'api' }

kurenn commented 7 years ago

@AndreiMotinga that is weird, but hey, as long as you made it worked. Should I close this issue then?