Angularjs:通过服务将数据发布到rails服务器

时间:2014-08-30 13:29:44

标签: ruby-on-rails angularjs

我想将angularjs中的数据发送到rails服务器。为此,我有一个angularjs服务,我使用GETPOSTDELETEUPDATE方法。我可以使用GET方法,但对于其他方法我无法使用,因为我必须将参数发送到服务器,但我不能这样做。

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' }
        });
    });

我通过以下代码在rails服务器中获取数据:

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};
    });

但我在rails服务器中遇到错误:

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. 我想向rails服务器发送delete方法。我知道我必须将record.id发送到服务器,我使用下面的类型:

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

    但我在服务器中遇到以下错误:

    在2014-08-30 19:01:21 +0430开始为127.0.0.1删除“/ api / record” ActionController :: RoutingError(没有路由匹配[DELETE]“/ api / record”): 我在recordService.js中修正了正确的网址,但我不知道为什么请求再次发送到网址之前。问题出在哪里?

1 个答案:

答案 0 :(得分:1)

看起来您正在成功发出请求,最后一行表示已发出POST请求并转到正确的控制器和操作。

问题是参数很强。您需要将name添加到已过滤的参数列表中。

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

此外,rails还需要采用以下格式的参数:{ 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,则可以省略网址中的.json。像这样:

class Api::V1::RecordController < Api::V1::BaseController
  respond_to :json
  ...
end
相关问题