ruby-on-rails - 由于 PATCH 路由错误,无法更新实例
问题描述
我正在尝试更新游戏实例,但即使我正在为游戏提供所有资源,我也会收到路由错误。
Started PATCH "/games" for ::1 at 2020-01-16 20:18:21 -0500
ActionController::RoutingError (No route matches [PATCH] "/games"):
这是我的路线.rb
Rails.application.routes.draw do
resources :games
end
这是我在游戏控制器中的更新方法
def update
game = Game.find_by(id: params[:id])
if game.update(game_params)
render json: { message: "Accepted initials." }
else
render json: { message: "Didn't accept initials" },
status: :not_acceptable
end
end
private
def game_params
params.require(:game).permit(
:score,
:initials,
:total_calories
)
end
如果有帮助,这是我的 fetch PATCH 请求
fetch("http://localhost:3000/games", {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Accept: "application/json"
},
body: JSON.stringify({
game: {
score: this.props.state.currentGame.score,
initials: this.state.initials,
total_calories: this.props.state.currentGame.total_calories
}
})
});
解决方案
您的补丁路线是“ http://localhost:3000/games/:id ”,而不是“ http://localhost:3000/games ”。
所以应该是...
fetch("http://localhost:3000/games/" + "<find a way to get game id>", {
推荐阅读
- apache-kafka - 我的 MirrorMaker2 如何镜像消费者偏移量,为什么这么慢?
- rendering - 在 VTK / Paraview 渲染中关闭透明对象的阴影
- html - 在 css 网格中缩放图像,但宽度和高度会增加大小
- symfony - 如何将 current() 传递给 hautelook 灯具中的模板
- c++ - 如何在 C++ 中装饰递归函数
- mule - 如何在 Anypoint Studio 中导入 Mule 连接器?
- c# - 在没有构造函数的类文件(不是组件)中注入依赖服务,在 blazor 中具有某种继承的注入属性?
- typescript - 在 TypeScript 中使用 rest 运算符删除了可选参数
- unity3d - Forge ARKit - 从 Navisworks 到 Unity
- google-analytics - DataLayer 变量/自定义维度仅适用于特定页面(单页应用)