0
我有一個模型before_create
和before_update
回調,可以return false
。當回調失敗時(如預期),模型不會保存,但是我的控制器重定向到未創建模型的索引路徑(而不是渲染新模板)。我的代碼是:瞭解Rails中的respond_with`
class Person
before_create :reversify
before_update :reversify
def reversify
return false if self.name.blank?
self.name = self.name.reverse
end
end
class PeopleController < ApplicationController
respond_to :html
def new
@person = Person.new
respond_with(@person)
end
def create
@person = Person.create(params[:person])
respond_with(@person)
end
end
感謝丹。對不起,我沒有指定上面的例子大大簡化了(我實際上正在與支付處理服務集成),但我最終放棄使用`responds_with`並回復到`respond_to`塊。乾杯。 – 2011-01-28 05:58:02