undefined method `marked_for_destruction?' CarrierWave, RMagick
Asked Answered
C

2

6

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
Carrasco answered 14/12, 2016 at 11:28 Comment(0)
O
1

If you're using Rails 5:

Open up new_framework_defaults.rb and change:

Rails.application.config.active_record.belongs_to_required_by_default = true

to

Rails.application.config.active_record.belongs_to_required_by_default = false

config.active_record.belongs_to_required_by_default is a boolean value and controls whether a record fails validation if belongs_to association is not present.

Oblate answered 6/1, 2018 at 18:43 Comment(4)
Thanks, could you improve your answer by writing that this is because Rails5 belongs_to required flag set to true 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 easilyBernadettebernadina
As a hobbyist, I agree with popStar. You're recommending I make a change to Rails without really explaining what I'm doing or what this method is doing. Feels really dangerous.Canzone
You just may put it in config/application.rb inside of class Apllication < Rails::Aplication end - Block: config.active_record.belongs_to_required_by_default = falseAllotropy
I did this in application.rb and it workedBoreal
S
0

Inside your the *_uploader.rb file just write the function:

  def marked_for_destruction?

  end
Stauder answered 20/12, 2019 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.