Rails 4.1 AngularJS将url从问号更改为斜杠

时间:2014-06-08 14:10:31

标签: angularjs ruby-on-rails-4

我关注this tutorial当我尝试实施Delete功能时,我发现rails无法检测到我的删除路径。在服务器日志中:

Started DELETE "/api/surveys?serviceId=19" for 127.0.0.1 at 2014-06-08 22:03:32 +0800

ActionController::RoutingError (No route matches [DELETE] "/api/surveys"):
  actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

我想将删除路径设置为:

/api/surveys/19

/app/assets/views/surveys/index.html.erb

<div ng-controller="SurveysCtrl">
<ul>
    <li ng-repeat="survey in surveys">
      {{ survey.theme_id }}, {{ survey.name }}, {{ survey.description }} 
      <a href='' ng-click="deleteSurvey(survey.id,$index)">Delete</a>      
    </li>
</ul>

/app/controllers/surveys_controller.rb

respond_to :json, :html

def destroy
  respond_with Survey.destroy(params[:id])
end

/app/assets/js/angular/controllers/surveys_ctrl.js

app.controller('SurveysCtrl', ['$scope', 'Survey', function($scope, Survey) {
  $scope.surveys = Survey.all();

  $scope.deleteSurvey = function(id,idx) {
     $scope.surveys.splice(idx,1);
     return Survey.delete(id);
  };
}]);

/app/assets/js/angular/services/surveys.js

app.factory('Survey', ['$resource', function($resource) {

function Survey() {
  this.service = $resource('api/surveys/:surveyId', {surveyId: '@id'});
};

Survey.prototype.all = function() {
  return this.service.query();
};

Survey.prototype.delete = function(stId) {
  this.service.remove({serviceId: stId})
};

return new Survey;

}]);

rake routes的输出:

      Prefix Verb   URI Pattern                     Controller#Action
     surveys GET    /surveys(.:format)              surveys#index
             POST   /surveys(.:format)              surveys#create
  new_survey GET    /surveys/new(.:format)          surveys#new
 edit_survey GET    /surveys/:id/edit(.:format)     surveys#edit
      survey GET    /surveys/:id(.:format)          surveys#show
             PATCH  /surveys/:id(.:format)          surveys#update
             PUT    /surveys/:id(.:format)          surveys#update
             DELETE /surveys/:id(.:format)          surveys#destroy
             GET    /api/surveys(.:format)          surveys#index {:format=>:json}
             POST   /api/surveys(.:format)          surveys#create {:format=>:json}
             GET    /api/surveys/new(.:format)      surveys#new {:format=>:json}
             GET    /api/surveys/:id/edit(.:format) surveys#edit {:format=>:json}
             GET    /api/surveys/:id(.:format)      surveys#show {:format=>:json}
             PATCH  /api/surveys/:id(.:format)      surveys#update {:format=>:json}
             PUT    /api/surveys/:id(.:format)      surveys#update {:format=>:json}
             DELETE /api/surveys/:id(.:format)      surveys#destroy {:format=>:json}
dashboard_index GET    /dashboard/index(.:format)      dashboard#index
sessions_new GET    /sessions/new(.:format)         sessions#new
sessions_create GET    /sessions/create(.:format)      sessions#create
sessions_destroy GET    /sessions/destroy(.:format)     sessions#destroy
       users GET    /users(.:format)                users#index
             POST   /users(.:format)                users#create
    new_user GET    /users/new(.:format)            users#new
   edit_user GET    /users/:id/edit(.:format)       users#edit
        user GET    /users/:id(.:format)            users#show
             PATCH  /users/:id(.:format)            users#update
             PUT    /users/:id(.:format)            users#update
             DELETE /users/:id(.:format)            users#destroy
       login GET    /login(.:format)                login#index
      logout GET    /logout(.:format)               sessions#destroy
     choices GET    /choices(.:format)              choices#index
             POST   /choices(.:format)              choices#create
  new_choice GET    /choices/new(.:format)          choices#new
 edit_choice GET    /choices/:id/edit(.:format)     choices#edit
      choice GET    /choices/:id(.:format)          choices#show
             PATCH  /choices/:id(.:format)          choices#update
             PUT    /choices/:id(.:format)          choices#update
             DELETE /choices/:id(.:format)          choices#destroy
   questions GET    /questions(.:format)            questions#index
             POST   /questions(.:format)            questions#create
new_question GET    /questions/new(.:format)        questions#new
edit_question GET    /questions/:id/edit(.:format)   questions#edit
    question GET    /questions/:id(.:format)        questions#show
             PATCH  /questions/:id(.:format)        questions#update
             PUT    /questions/:id(.:format)        questions#update
             DELETE /questions/:id(.:format)        questions#destroy
       types GET    /types(.:format)                types#index
             POST   /types(.:format)                types#create
    new_type GET    /types/new(.:format)            types#new
   edit_type GET    /types/:id/edit(.:format)       types#edit
        type GET    /types/:id(.:format)            types#show
             PATCH  /types/:id(.:format)            types#update
             PUT    /types/:id(.:format)            types#update
             DELETE /types/:id(.:format)            types#destroy
      themes GET    /themes(.:format)               themes#index
             POST   /themes(.:format)               themes#create
   new_theme GET    /themes/new(.:format)           themes#new
  edit_theme GET    /themes/:id/edit(.:format)      themes#edit
       theme GET    /themes/:id(.:format)           themes#show
             PATCH  /themes/:id(.:format)           themes#update
             PUT    /themes/:id(.:format)           themes#update
             DELETE /themes/:id(.:format)           themes#destroy
  home_index GET    /home/index(.:format)           home#index
        root GET    /                               home#index

1 个答案:

答案 0 :(得分:1)

根据文档,我在代码中注意到的唯一区别是survey.js

中的这一行
this.service.remove({serviceId: stId})

应该是

this.service.remove({surveyId: stId})

错误也是如此。请将其更新如下。

/app/assets/js/angular/services/surveys.js:

app.factory('Survey', ['$resource', function($resource) {

function Survey() {
  this.service = $resource('api/surveys/:surveyId', {surveyId: '@id'});
};

Survey.prototype.all = function() {
  return this.service.query();
};

Survey.prototype.delete = function(stId) {
  this.service.remove({surveyId: stId})
};

return new Survey;

}]);