3

在 Rails4 中,当使用带有更新方法“PATCH”的 Angular ngResource 时,出现服务器错误:

ActionController::ParameterMissing (param not found: page):
 app/controllers/json_api/pages_controller.rb:39:in `filter_page_params'
 app/controllers/json_api/pages_controller.rb:17:in `update'

window.app.factory 'Page', ($resource) ->
  ...
ReStFull: ( ->
  return $resource('/json/pages/:id', {id: '@id'}, {update: {method: 'PATCH'}}) 
 )

只有 PUT 有效!

我的 Rails4 控制器操作如下所示:

def update
  if @page.update_attributes(filter_page_params)
    render json: @page
  else
    render nothing: true, status: 400
  end
end

强参数私有函数:

def filter_page_params
  params.require(:page).permit(:parent_id, :name, :grid_layout_id)
end

更新有一个预加载钩子,@page 已经从数据库加载

有人知道是什么原因导致角度中断请求吗?

我感谢任何帮助。亲切的问候,亚历克斯

4

1 回答 1

3

当您在 angular-resource 中使用 PATCH 请求时,标头的 Content-Type 将设置为“application/xml”,我假设您的后端仅响应 json,这就是您收到错误的原因。您需要手动将其设置为“application/json”。

在稳定版本中执行此操作的一种方法是使用 $httpProvider 设置默认标头

$httpProvider.defaults.headers.common["Content-Type"] = 'application/json'

在不稳定版本 1.1.1+ 中,您可以直接在 $resource 中设置它,我没有尝试过,但这里提到:https ://groups.google.com/forum/#!msg/angular/33kV8fjFcME/0f0y2mL2DBgJ

$resource '/users/:id',
  { id: '@id' }
  update:
    method: 'PATCH'
    headers: { 'Content-Type': 'application/json' }
于 2013-08-29T05:29:17.087 回答