I am transferring the ability to crop images uploaded by Carrierwave. Here is a RailsCast video on Youtube that I am following.
But after including RMagick in the bootloader, I got:
undefined method `marked_for_destruction?' for #<ImageUploader:0x007fe86634fcf0>
What the hell was that I was thinking. I did not call this method anywhere. But if it is not defined, let's define it! And it worked! But later I checked more about this method and found that it was built into the Active Record Autosave Association module. And from the docs about this method:
Returns whether this record will be destroyed as part of the parents save the transaction.
It is only useful if the autosave option for the parent of the associated model is enabled for this parameter.
But I did not pass autosave: true any object!
So, my first question is - was this somehow done by default?
2 - in the RailsCast manual, he did not define this method. Why should I have?
3 - I pass my code below. Any bugs?
4 - if possible, can someone explain how this process works in general?
Thank you very much!
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