【问题标题】:How does "[attribute]_changed?" work with CarrierWave mount_uploader :[attribute]?“[attribute]_changed”是如何变化的?使用 CarrierWave mount_uploader :[attribute]?
【发布时间】:2019-10-14 17:44:02
【问题描述】:

我的模型中有 CarrierWave 上传器。

before_validation :parse_template_file, if: :template_file_changed?
mount_uploader :template_file

我上面的代码无法抛出NoMethodError template_file_changed?

如何在保存记录之前检查文件是否已更改?

【问题讨论】:

    标签: ruby-on-rails ruby carrierwave


    【解决方案1】:

    some_attr_changed? 是一个实例方法,因此您需要在模型的实例上调用它。

    所以,而不是这个......

    before_validation :parse_template_file, if: :template_file_changed?
    

    试试这样的...

    before_validation :parse_template_file, if: :file_changed?
    
    private
    
    def file_changed?
      self.template_file_changed?  # self can be implied
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多