0
我想處理,其中用戶輸入信息錯誤的情況,所以我有如下大致的路徑:如何在更新屬性之前添加錯誤?
class Thing < AR
before_validation :byebug_hook
def byebug_hook
byebug
end
end
thing = Thing.find x
thing.errors.add(:foo, "bad foo")
# Check byebug here, and errors added
if thing.update_attributes(params)
DelayedJobThatDoesntLikeFoo.perform
else
flash.now.errors = #...
end
byebug for byebug_hook> errors.messages #=> {}
本來我想,也許該模型運行其自己的驗證和覆蓋的那些我補充說,但正如你可以看到,即使我添加前掛鉤錯誤丟失,我不知道是什麼原因造成的
實際解決方案 所以,@SteveTurczyn是正確的,錯誤需要發生在某些地方,在這種情況下,在我的控制器中調用的服務對象
我所做的更改是
class Thing < AR
validate :includes_builder_added_errors
def builder_added_errors
@builder_added_errors ||= Hash.new { |hash, key| hash[key] = [] }
end
def includes_builder_added_errors
builder_added_errors.each {|k, v| errors.set(k, v) }
end
end
and in the builder object
thing = Thing.find x
# to my thinking this mirrors the `errors.add` syntax better
thing.builder_added_errors[:foo].push("bad foo") if unshown_code_does_stuff?
if thing.update_attributes(params)
DelayedJobThatDoesntLikeFoo.perform
else
flash.now.errors = #...
end
我想在你試圖讓你的代碼片斷簡潔你做了它難以遵循。無論哪種情況,我認爲你會在[這裏]找到你的問題的答案(http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations) – Iwnnay