1
控制器:軌道狀態代碼和XML
class CategoriesController < ApplicationController
def create
@category = Category.create(...)
respond_to do |format|
if @category.save
format.xml { :status => :created }
else
format.xml { :status => :unprocessable_entity }
end
end
end
end
查看:
xml.instruct! :xml, :version => "1.0"
xml.response do
xml.status(STATUS)
xml.code(STATUS CODE)
end
正如你可以看到我把我的創建控制器操作的內部狀態代碼。我的問題是如何在視圖內讀取這個狀態碼(例如STATUS CODE應該是一個200的數字,STATUS應該是像「OK」,「Unauthorized」這樣的字符串)。我知道我可以創建一個變量,例如@status ='ok',但我不想重複代碼。 Thx爲答案!
哼哼......多餘的是的,我想你是對的。我只是認爲這將是很好的alwais有一些額外的錯誤信息的迴應。 – xpepermint 2010-02-26 15:02:53