Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在AngularJS服务中为更新API请求包装参数_Ruby On Rails_Ruby_Angularjs_Api_Service - Fatal编程技术网

Ruby on rails 在AngularJS服务中为更新API请求包装参数

Ruby on rails 在AngularJS服务中为更新API请求包装参数,ruby-on-rails,ruby,angularjs,api,service,Ruby On Rails,Ruby,Angularjs,Api,Service,我正在尝试使用AngularJS和API进行更新 服务:expense.js angular .module('timeTrackerApp.services',[]) .service('Expense', ['$resource', function ($resource) { return $resource('/api/v1/expenses/:id', {id:'@id'}, { 'update': { method: 'PUT'

我正在尝试使用AngularJS和API进行更新

服务:expense.js

angular
  .module('timeTrackerApp.services',[])
  .service('Expense', ['$resource', function ($resource) {
    return $resource('/api/v1/expenses/:id', {id:'@id'}, {
      'update': {
        method: 'PUT'
      },
      'destroy': {
        method: 'DELETE'
      }
    })
  }])
控制者:费用控制者.rb

def permitted_params
   params.require(:expense).permit(:name, :price)
end
所以预期的JSON格式是{expense:{name:“value”,price:value}
但是我得到了{name:“value”,price:value}


那么有谁能帮我将其包装到根节点(费用)?

当控制器名称与模型名称匹配时,Rails会自动包装参数

如果出现故障,您可以在控制器中手动执行:

 wrap_parameters :expense, include: [:name, :price]
因此,如果您收到:

 { name: 'name', price: 'price' }
控制器将为您提供:

 { name: 'name', price: 'price', expense: { name: 'name', price: 'price' } }

因此,这个服务器端非常简洁。

你应该用ruby标记。是
费用
模型吗?是的,你需要更多信息吗?
在Rails中自动包装参数
名称空间参数,所以我猜
名称
价格
不是模型的属性,我需要以不同的格式发送json,我需要包括根节点,而不仅仅是参数