gpt4 book ai didi

ruby - 重定向到 @timecard 结果为 'no route matches [PATCH] "/timecards"

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:18 24 4
gpt4 key购买 nike

无法解决这个路由问题:我从我的更新 Controller 操作重定向到修改后的@timecard,并收到以下错误:没有路由匹配 [PATCH] "/timecards"

为什么 Rails 尝试使用 PATCH 重定向到 timecards#index?

这不是我重定向到最近编辑的@timecard 的方式吗?

timecards_controller.rb中:

  def edit
@timecard = Timecard.find(params[:id])
end

def update
@timecard = Timecard.find(params[:id])
@timecard.update_attributes!(timecard_params)
redirect_to @timecard
end

rake routes的输出:

$ rake routes
Prefix Verb URI Pattern Controller#Action
timecard_timecard_entries GET /timecards/:timecard_id/timecard_entries(.:format) timecard_entries#index
POST /timecards/:timecard_id/timecard_entries(.:format) timecard_entries#create
new_timecard_timecard_entry GET /timecards/:timecard_id/timecard_entries/new(.:format) timecard_entries#new
edit_timecard_timecard_entry GET /timecards/:timecard_id/timecard_entries/:id/edit(.:format) timecard_entries#edit
timecard_timecard_entry GET /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#show
PATCH /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#update
PUT /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#update
DELETE /timecards/:timecard_id/timecard_entries/:id(.:format) timecard_entries#destroy
timecards GET /timecards(.:format) timecards#index
POST /timecards(.:format) timecards#create
new_timecard GET /timecards/new(.:format) timecards#new
edit_timecard GET /timecards/:id/edit(.:format) timecards#edit
timecard GET /timecards/:id(.:format) timecards#show
PATCH /timecards/:id(.:format) timecards#update
PUT /timecards/:id(.:format) timecards#update
DELETE /timecards/:id(.:format) timecards#destroy
root GET / timecards#index

最佳答案

当您调用 redirect_to 时,您应该指定一个路径...不一定是对象本身。

尝试,

def update
@timecard = Timecard.find(params[:id])
@timecard.update_attributes!(timecard_params)
redirect_to timecard_path(@timecard)
end

关于ruby - 重定向到 @timecard 结果为 'no route matches [PATCH] "/timecards",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640508/

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