2014-08-30 134 views
1

我想從angularjs發送數據到rails服務器。爲此,我使用了012j,POST,DELETE,UPDATE方法的angularjs服務。我可以使用GET方法,但對於其他方法我無法使用,因爲我必須將參數發送到服務器,但我無法執行此操作。Angularjs:通過服務發佈數據到rails服務器

record.js

var app = angular.module('app'); 
app.controller('RecordCtrl',['$scope','Session','Records', function($scope, Session, Records){ 
    $scope.records = Records.index(); 
}]); 

recordService.js

'use strict'; 
var app = angular.module('recordService', ['ngResource']); 
//angular.module('recordService', ['ngResource']) 
app.factory('Records', function($resource) { 
    return $resource('/api/record.json', {}, { 
     index: { method: 'GET', isArray: true}, 
     create: { method: 'POST' } 
    }); 
}) 
    .factory('Secure', function($resource){ 
     return $resource('/api/record/:record_id.json', {}, { 
      show: { method: 'GET' }, 
      update: { method: 'PUT' }, 
      destroy: { method: 'DELETE' } 
     }); 
    }); 

,我通過下面的代碼獲得軌服務器數據:

class Api::V1::RecordController < Api::V1::BaseController 
    def index 
    respond_with(Record.all) 
    end 

    def show 
    @data = Record.find(params[:id]).to_json() 
    respond_with(@data) 
    end 

    def update 
    @data = Record.find(params[:id]) 
    respond_to do |format| 
     if @data.update_attributes(record_params) 
     format.json { head :no_content } 
     else 
     format.json { render json: @data.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def create 
    @data = Record.create(record_params) 
    @data.save 
    respond_with(@data) 
    end 

    def destroy 
    @data = Record.find(params[:id]) 
    @data.destroy 
    respond_to do |format| 
     format.json { head :ok } 
    end 
    end 

    private 
    def record_params 
    params.require(:record).permit(:name) 
    end 
end 

我不知道我怎麼能發送方法從angularjs控制器到rails服務器。我嘗試下面的代碼,但我沒有成功:

Records.create(function() { 
     //"name" is the name of record column. 
     return {name: test3}; 
    }); 

但我得到以下錯誤在軌服務器:

Started POST "/api/record.json" for 127.0.0.1 at 2014-08-30 17:55:27 +0430 
Processing by Api::V1::RecordController#create as JSON 
  1. 我怎樣才能解決這個問題呢?我怎樣才能發送參數到rails服務器?
  2. 我想發送delete方法到rails服務器。我知道我要送record.id到服務器,我用以下類型:

    //type 1 
    var maskhare = { record_id: 4}; 
    Secure.destroy(function(){ 
        return maskhare.json; 
    }); 
    //type 2 
    Secure.destroy(4); 
    

    但我得到以下錯誤在服務器:

    開始刪除「/ API /記錄」在2014-08 127.0.0.1 -30 19:01:21 +0430 ActionController :: RoutingError(沒有路由匹配[DELETE]「/ api/record」): 我修復了recordService.js中的正確url,但我不知道爲什麼請求發送到url之前再次。哪裏有問題?

回答

1

看起來您正在成功提出請求,最後一行表示發出POST請求並轉至正確的控制器和操作。

問題是參數很強。您需要將name添加到已過濾的參數列表。

private 
def record_params 
    params.require(:record).permit(:secure, :name) 
end 

而且軌道預計在以下格式的參數:{ record: {name: 'something"} }

要解決你的第二個問題

我會嘗試按照this recipe

這種替換代碼:

app.factory("Secure", function($resource) { 
    return $resource("/api/record/:id", { id: "@id" }, 
    { 
     'show': { method: 'GET', isArray: false }, 
     'update': { method: 'PUT' }, 
     'destroy': { method: 'DELETE' } 
    } 
); 
}); 

然後

Secure.destroy({id: 4}); 

請記住,如果你在你的控制器中添加respond_to :json則可以省略在URL中.json。像這樣:

class Api::V1::RecordController < Api::V1::BaseController 
    respond_to :json 
    ... 
end 
+0

的問題不是與軌所以這是一個開始 - Rails的期望,你會調用'刪除/ API /記錄/:id'但你正在看起來像這樣的請求'DELETE/api/record' – Ahmed 2014-08-30 15:33:06

+0

我編輯了我的答案,修復了AngularJS代碼。讓我知道這是否有效。 – Ahmed 2014-08-30 15:39:23

+0

你可能想要更新上面的代碼,以便我可以看到有什麼問題。這只是不發送身份證。 – Ahmed 2014-08-30 16:40:46

相關問題