2014-08-30 117 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