在模型更新期间访问CarrierWave模块中的模型属性

时间:2015-01-04 07:54:49

标签: ruby-on-rails ruby-on-rails-4 carrierwave minimagick

我正在尝试在模型更新期间访问CarrierWave上传器类中的模型属性。

当我更新模型时,CarrierWave运行一些过程来调整大小并创建原始图像的版本。

在此过程中,我想访问Photo params哈希中的default_mat参数,如下所示。

{"utf8"=>"✓", "authenticity_token"=>"+yregNfTKmy78MWFSLQc+z7ufjcs8nkdJSJlBdrTIds=", "photo"=>{"id_num"=>"jh860", "default_frame"=>"1", "show_bw_conversion"=>"true", "rotating_keyword"=>"picture", "image"=>#<ActionDispatch::Http::UploadedFile:0x007ff85768b490 @tempfile=#<Tempfile:/var/folders/jt/wm_zgby1701d31fwp0v9n86h0000gn/T/RackMultipart20150104-69760-sac29l>, @original_filename="1470.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"1470.jpg\"\r\nContent-Type: image/jpeg\r\n">, "image_cache"=>"", "is_active"=>"false", "title"=>"sdfsdfsdfd", "description"=>"<p>sdfsadf</p>", "category_ids"=>["", "1"], "photographer_id"=>"5", "film_type"=>"digital", "year_taken"=>"2015", "format_id"=>"1", "default_mat"=>"1"}, "commit"=>"Add Photo", "action"=>"create", "controller"=>"photos"}

如何从哈希中访问default_mat属性/值并在更新期间在CarrierWave上传器类中使用?

class ProductImageUploader < CarrierWave::Uploader::Base

 include CarrierWave::MiniMagick
 include CarrierWave::ImageOptimizer


 def frame_photo
  manipulate! do |img|
    # puts "i am trying to access the #{model.default_mat} value"
    mat_frame = MiniMagick::Image.open("http://s3-us-west-2.amazonaws.com/luminoto-modal/fog_black.jpg")
    mat_frame.rotate("90") if img.height > img.width
    result = mat_frame.composite(img) do |c|
      c.compose "Over"    # OverCompositeOp
      c.gravity "Center" # copy second_image onto first_image from (20, 20)
    end
    result.resize "525x525>"
    result
  end
end

1 个答案:

答案 0 :(得分:0)

您可以使用model关键字访问carrierwave中的相应模型对象。

def default_mat(picture)
  model.default_mat
end