11

我正在尝试通过合并来干燥控制器respond_with。当我这样做时,按照 Railscast 中的一些说明,我得到的东西大部分都在工作。问题在于删除资源后的重定向...应该重定向到people_url...但尝试加载特定资源。

我找到的示例代码看起来像这样......但它试图加载它刚刚删除的资源失败:

# app/controllers/people_controller.rb
class PeopleController < ApplicationController
  respond_to :html, :xml

  def destroy
    @person = Person.find(params[:id])
    flash[:notice] = 'Successfully deleted person.' if @person.destroy
    respond_with(@person)  # <== spec fails here
  end
end

将最后一行更改为respond_with(@people)也不起作用(尽管我希望它会......)

经过大量挖掘并尽我所能理解事情后,我确实让事情正常工作(至少看起来如此。规范通过。应用程序功能):

respond_with(@person, :location => people_url)  # <== now it works

那么,这是处理这个问题的正确方法吗?似乎在 response_with 背后的所有“魔法”都会知道它在删除后无法重定向到自己?我还认为这(7 种基本的 RESTful CRUD 方法之一)将是非常基本和基本的,因此会有很多示例......但除了那些建议代码不起作用的示例之外,我找不到很多示例我。

希望有人能帮助我理解这里发生的铁轨“魔法”,这样当我在路上遇到这种情况时,我不会感到惊讶。

4

1 回答 1

5

You are trying to respond with a resource that's deleted. That's what the problem is. In cases such as deletion, header-only responses work. Setting the request header status to :ok is enough.

head :ok
于 2011-02-15T06:02:08.403 回答