2014-08-30 113 views
0

我已經開始爲我的rails應用程序創建一個Api。我目前創建會話控制器日誌。沒有路由匹配DELETE會話Api

但由於某些原因,我得到這個錯誤

Started DELETE "/api/v1/sessions/?auth_token=6157d3673725013ebddbb5e26e8cd64756949110" 
for 127.0.0.1 at 2014-08-29 18:54:18 -0700 

ActionController::RoutingError (No route matches [DELETE] "/api/v1/sessions"): 

我不理解爲什麼會這樣。註銷似乎在實際的Web應用程序上完美工作。

我知道它可能需要一個ID根據耙路線,但我不知道如何實現這一點。

API控制器

module Api 
    module V1 
    class SessionsController < ApplicationController 
     skip_before_filter :verify_authenticity_token, 
         :if => Proc.new { |c| c.request.format == 'application/json' } 

     respond_to :json 

     def destroy 
     sign_out 
     render :status => 200, 
      :json => { :success => true, 
         :info => "Logged Out", 
         :data => {} } 
     end 

    end 
    end 
end 

控制器

class SessionsController < ApplicationController 

    def destroy 
    sign_out 
    redirect_to root_path 
    end 

end 

會議HELPER

def sign_out 
    current_user = nil 
    cookies.delete(:remember_token) 
end 

個ROUTES

### API Routes 
namespace :api, defaults: {format: 'json'} do 
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do 
    resources :sessions, only: [:new, :create, :destroy] 
    end 
end 

RAKE ROUTES

api_v1_sessions POST /api/v1/sessions(.:format)       
api/v1/sessions#create {:format=>"json"} 

api_v1_session DELETE /api/v1/sessions/:id(.:format)      
api/v1/sessions#destroy {:format=>"json"} 

回答

2

documentation

可以使用resource代替resources路線幫手。它用於爲您不使用ID訪問的單一資源創建路由。

namespace :api, defaults: {format: 'json'} do 
    namespace :v1, constraints: ApiConstraints.new(version: 1, default: true) do 
    resource :session, only: [:new, :create, :destroy] 
    end 
end 

,這將給你

GET  /session/new 
POST  /session 
DELETE /session 
+0

這是我現在使用的命令:捲曲-v -H '內容類型:應用程序/ JSON' -H '接受:應用/ JSON' - X DELETE http:// localhost:3000/api/v1/session/\?auth_token \ = 3b774e0d8f7abdf7dabfcbb45a6ff11be2288d2d – 2014-08-30 04:29:57

+0

但我仍然收到此錯誤消息:ActionController :: RoutingError(沒有路由匹配[DELETE]「/ api/v1/session」 ): – 2014-08-30 04:30:19

+1

而'rake routes'顯示'DELETE「/ api/v1/session」'路線? – Ahmed 2014-08-30 13:32:31