2017-01-08 26 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