仅保留用于发送POST请求的对象ID

时间:2016-04-28 08:28:16

标签: javascript angularjs

目前,我完成了多个UI选择功能,可以预先选择多个项目。

但是loan_plan.careers存储数组的对象以启用预先选择的功能。

因此,POST请求将在params中objects of array而不是数组的id。

我如何才能将POST请求安装在ruby的rails轨道上?

因为ROR希望params应该是loan_plan.career_ids = [2,4,56]

这是我目前的解决方法,它闻起来很糟糕。有什么好建议吗?

    $scope.$watch('loan_plan.careers', function(new_val, old_val) {
        if($scope.loan_plan){
            $scope.loan_plan.career_ids = _.map( new_val , function(num) { return num.id; });
        }
    });

inline

HTML

<ui-select multiple ng-model="loan_plan.careers" theme="bootstrap" ng-disabled="disabled">
<ui-select-match > {{$item.name}} </ui-select-match>
<ui-select-choices repeat="option in careersOptions | propsFilter: {name: $select.search} ">
<div ng-bind-html="option.name | highlight: $select.search"></div>
<small>
<span ng-bind-html="''+ option.description | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>

service.js

'use strict';
angular.module('LoanPlanModule', [])
    .factory('LoanPlanService', ['$resource',
        function($resource, $http) {
            var LoanPlan = $resource('/api/v1/banks/:bank_id/loan_plans/:loan_plan_id', {
                bank_id: '@bank_id',
                loan_plan_id: '@loan_plan_id'
            }, {
                 update: { method:'PUT' },
                 create: { method: 'POST' },
                 delete: { method: 'DELETE'}
            });

            return LoanPlan;
        }
    ]);

0 个答案:

没有答案