2

Can't figure out this routing problem: I redirect from my update controller action to the modified @timecard, and get the following error: No route matches [PATCH] "/timecards"

Why does rails try to redirect using PATCH and to timecards#index?

Is that not how I redirect to the recently edited @timecard?

In 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

Output of 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
4

2 回答 2

1

当你打电话时,redirect_to你应该指定一个路径..不一定是对象本身。

尝试,

def update
  @timecard = Timecard.find(params[:id])
  @timecard.update_attributes!(timecard_params)
  redirect_to timecard_path(@timecard)
end
于 2013-09-05T15:38:26.523 回答
0

问题自然就在眼前。感谢@sircapsalot 将我指向日志,这表明确实调用了不正确的路由(来自视图中的不正确链接)。

于 2013-09-22T17:07:30.107 回答