如何解决“尝试上传图像时,除非保存父项”,否则无法调用“创建”错误?

时间:2015-03-31 17:56:49

标签: ruby-on-rails carrierwave

当我尝试将图像上传到我的rails应用程序时,我会检查它是否低于3百万像素并删除它是否低于该值。如果图像高于3MP,则可以。但如果它低于3mp,它会给我这个错误You cannot call create unless the parent is saved

照片创建功能:

def create
    @photo = current_user.photos.build(file: params[:file])
    if @photo.save!(:validate => false)
        respond_to do |format|
            format.json{ render :json => @photo }
        end
    end
end

检查百万像素功能:

after_create :check_mp
before_save :set_mrdocs_rejects
before_create :new_workflow_state
validates :title, :description, :category_id, :tag_list, presence: true
before_save :custom_corrections
after_save :accept_or_reject, :submit_photo
validate :custom_validations

def check_mp
    @min_mp = Setting.find_by_key('min_megapixel').value.to_i
    @mp = ((self.width * self.height) / 1000000.0).round
    if @mp < @min_mp
        rejected = self.user.rejected
        rejected << self.originalname
        self.user.update_attribute(:rejected, rejected)

        self.destroy
    end
end

可能导致错误的原因是什么?

0 个答案:

没有答案