2014-04-26 60 views
0

我想安裝使用Rails這樣的服務器端的認證服務:角資源傳遞不必要的參數

的Javascript:

angular.module('myApp',['ngResource']) 
.factory("Session",['$resource',function($resource){ 
    return $resource('/sessions',{}, { 
     create: {method: 'POST', isArray: false}, 
     destroy: {method:'DELETE', url: '/sessions/destroy.json' } 
    }); 
}]) 
.controller("LoginController",['$scope','Session',function($scope,Session){ 
    $scope.model = {}; 

    $scope.login = function(username,password){ 
    //Session.save({username: username, password: password}).then(function(success){ 
    // $scope.model.user = success.data; 
    // console.log("New Session"); 
    //},function(error){ 
    // console.log("Login failed!!!"); 
    // console.log(error); 
    //}); 
    $scope.model.user = { 
     first: "John", 
     last: 'Doe' 
    }; 
    }; 

    $scope.logout = function(){ 
    Session.destroy().then(function(success){ 
     $scope.model.user = {}; 
     console.log("Session Deleted!!!"); 
    },function(error){ 
     console.log("Error in Session delete..."); 
     console.log(error); 
    }) 
    }; 

}]); 

HTML:

<div class="navbar navbar-inverse" role="navigation" ng-controller="LoginController"> 
     <div class="navbar-right navbar-form " style="color:white" ng-show="model.user"> 
     <span class="glyphicon glyphicon-user"></span> 
     <span>{{ model.user.first + ' ' + model.user.last}}</span> 
     <button ng-click="logout()" type="submit" class="btn btn-danger navbar-btn">Logout</button> 
     </div> 
     <div class="navbar-form navbar-right" role="login" ng-hide="model.user"> 
     <div class="form-group"> 
      <a ng-href="#">new user? </a> 
      <a ng-href="#"> forgot password?</a> 
     </div> 
     <form class="form-group" name="loginForm" ng-submit="login()"> 
      <input type="text" class="form-control" placeholder="username" ng-model="username" /> 
      <input type="password" class="form-control" placeholder="password" ng-model="password" /> 
      <button type="submit" class="btn btn-primary navbar-btn">Login</button> 
     </form> 
     </div> 
    </div> 

但是,當我做註銷請求,角度是把一個額外的參數看到rails服務器日誌:

Started DELETE "/sessions/destroy.json" for 127.0.0.1 at 2014-04-26 18:30:30 -0400 
Processing by SessionsController#destroy as JSON 
    Parameters: {"id"=>"destroy"} 

這是怎麼發生的?

回答

0

試試這個:

angular.module('myApp',['ngResource']) 
.factory("Session",['$resource',function($resource){ 
    return $resource('/sessions/:action',{}, { 
     create: {method: 'POST', isArray: false}, 
     destroy: {method:'DELETE', params: { action: 'destroy.json' } } 
    }); 
}])