I am passing the ability to crop images, uploaded by Carrierwave. Here is RailsCast video on Youtube which I am following.
But after including RMagick in uploader, I received:
undefined method `marked_for_destruction?' for #<ImageUploader:0x007fe86634fcf0>
What a heck is this I thought. I haven't called this method anywhere. But if it is not defined, lets define it! And it worked! But later I checked more about this method and found that it is built in in Active Record Autosave Association module. And from docs, about this method:
Returns whether or not this record will be destroyed as part of the parents save transaction.
Only useful if the :autosave option on the parent is enabled for this associated model.
But I didn't passed autosave: true
to any object!
So, my first question - was it done by default somehow?
2 - on RailsCast tutorial he didn't defined this method. Why I had to?
3 - I pass my code bellow. Any errors?
4 - if possible, could anyone explain how this process works, in general?
Many thanks!
product.rb:
has_one :image
validates :image, presence: true
mount_uploader :image, ImageUploader
products_controller.rb:
def create
@product = Product.new(product_params)
@product.category_id = params[:category_id]
@product.user_id = current_user.id
respond_to do |format|
if @product.save
if params[:product][:image].present?
format.html { render :crop }
else
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
end
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
image_uploader.rb:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
def marked_for_destruction?
@marked_for_destruction
end
def mark_for_destruction
@marked_for_destruction = true
end
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :large do
resize_to_limit(600,600)
end
end
belongs_to
required flag set totrue
by default and how is it related to my issue. I bet folks will be thankful for that. I am not working on that project anymore, so cant test this easily – Bernadettebernadina