1
我已經配置application.rb中具有下列選項添加索引錯誤的嵌套模型:覆蓋全局index_errors選項
config.active_record.index_nested_attribute_errors = true
我有很多的模式和它的偉大工程,但我要改變這種行爲的單一模式,就像這樣:
# frozen_string_literal: true
class User < ApplicationRecord
has_many :addresses,
inverse_of: :user,
dependent: :destroy,
index_errors: false #note here
end
但是它不按預期工作,換句話說,這些錯誤仍然會是這樣的:
{"addresses_attributes[0].zip_code":[{"error":"blank"}]}
由於我重寫(或沒有)的全局配置,我期望這樣的:
{"addresses_attributes.zip_code":[{"error":"blank"}]}
好像我不能從application.rb中覆蓋全局配置。有什麼想法嗎?
感謝更新錯誤。沒有辦法在模型級重寫這個配置嗎?我嘗試在我的模型中執行'Rails.configuration.active_record.index_nested_attribute_errors = false',它會編譯,但不覆蓋全局設置。 –
從未嘗試過,但Rails會非常脆弱,允許配置在應用程序啓動後設置/重置。你爲什麼不重寫這個錯誤的密鑰,因爲解決方法 –
你可以這樣做來改變錯誤: –