gpt4 book ai didi

rest - Rails4 : Server error when using AngularJS resource with update method = PATCH

转载 作者:行者123 更新时间:2023-12-02 00:05:59 25 4
gpt4 key购买 nike

在 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 Controller 操作如下所示:

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

强参数私有(private)函数:

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

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

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

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

最佳答案

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

在稳定版本中,一种方法是使用 $httpProvider 设置默认 header

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

在 unstable version 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' }

关于rest - Rails4 : Server error when using AngularJS resource with update method = PATCH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18363469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com