更新属性不起作用

时间:2014-04-10 15:54:04

标签: ruby-on-rails carrierwave inherited-resources

我有两列:

background_image_id (which is a relation)
background_image (which is a copy of that image, with various versions)

在我的before_update方法中,我必须将Carrierwave文件从Medium.find(background_image_id)复制到background_image列。

我不能这样做。 NIL存储在数据库中。不知道为什么。


控制器:

def update
  if params[:profile].blank? || params[:profile][:background_image_id].blank?
    flash[:alert] = 'Please select image'
    redirect_to edit_candidate_background_image_path
  else
    resource.background_image_id = params[:profile][:background_image_id]

    if resource.save
      flash[:notice] = 'Profile was updated'
     else
      flash[:alert] = "Can't update profile"
    end

    redirect_to edit_candidate_background_image_path
  end
end

型号:

before_update :copy_background_image, if: :background_image_id_changed?  
...

def copy_background_image
  medium = Medium.find(background_image_id)
   if medium.present?
    self.background_image = medium.image.file
    puts self.inspect # outputs a model where background_image is nil
    puts self.background_image.inspect # outputs an uploader with correct image PRESENT
  end
end

0 个答案:

没有答案