我有一个具有虚拟属性的模型:
attr_accessible :published_at
def published_at_text
I18n.localize(published_at, format: :long_no_day_with_seconds) if published_at
end
def published_at_text=(text)
self.published_at = Chronic.parse(text)
end
这在单元测试中工作正常,但在视图中更改了 published_at_text 字段时不会保存。我试过使用attr_accessible :published_at_text
, 并添加published_at_will_change!
到 setter 方法,但我无法让它工作。
这development.log
表明已将 published_at_text 的更改值传入,但在 setter 中添加对 Rails.logger 的调用似乎表明它甚至没有被调用。
我在这里想念什么?