2017-01-08 28 views
1

我目前正在使用rails開發REST API,請參閱本教程http://apionrails.icalialabs.com/book/chapter_two。我正在使用郵遞員測試路線,但我得到了所有操作的404響應代碼。HTTPS狀態:404未在rails中找到所有路由的REST API

This is my available routes in screentshot

這是我的路線文件的內容:

Rails.application.routes.draw do 
    devise_for :users 
    namespace :api, defaults: { format: :json }, 
      constraints: { subdomain: 'api'}, path: '/' do 

     resources :users, :only => [:show, :create, :update, :destroy] 
     resources :sessions, :only => [:create, :destroy] 
    end 
end 

我使用的設計對用戶進行認證。

+0

做你的郵遞員使得與子域名的請求包括在內? – ClassyPimp

回答

0

也一直在經歷這個教程。有同樣的問題 - 我所有的路線都不起作用,所以我不能對郵差進行任何請求。

我所做的就是消除來自routes.rb

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

而按照教程,你可能想的範圍和版本添加到您的路線:

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

但是它給你(不知道你跟隨教程有多接近)

所以你的代碼看起來像這樣:

Rails.application.routes.draw do 
    devise_for :users 
    namespace :api, defaults: { format: :json } do 
     #optional 
     scope module: :v1, 
     constraints: ApiConstraints.new(version: 1, default: true) do 
     resources :users, :only => [:show, :create, :update, :destroy] 
     resources :sessions, :only => [:create, :destroy] 
     end 
    end 
end 

在那之後我的要求開始工作方式如下:

localhost:3000/api/users/1 
+0

它的工作,謝謝。 – mbd

+0

很高興幫助!如果您將我的答案標記爲正確的話,那將會很好,很有幫助。 – VAD