我傳遞了裁剪圖像的能力,由Carrierwave上傳。這是我正在關注的RailsCast video on Youtube。未定義的方法`marked_for_destruction?' CarrierWave,RMagick
但包括RMagick在上傳後,我收到:
undefined method `marked_for_destruction?' for #<ImageUploader:0x007fe86634fcf0>
什麼赫克這是我的想法。我沒有在任何地方調用這個方法。但如果沒有定義,我們來定義它!它的工作!但後來我查了更多關於這種方法,發現它內置於Active Record Autosave Association模塊。從文檔中,關於此方法:
返回此記錄是否會作爲 父母保存事務的一部分而被銷燬。
僅當對此 關聯模型啓用父項的自動保存選項時纔有用。
但我沒有通過autosave: true
任何對象!
所以,我的第一個問題 - 它默認以某種方式完成?
2 - 關於RailsCast教程,他沒有定義這個方法。爲什麼我必須?
3 - 我通過我的代碼波紋管。任何錯誤?
4 - 如果可能的話,任何人都可以解釋一下這個過程的工作原理嗎?
非常感謝!
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
謝謝你,你能寫,這是提高你的答案,因爲Rails5'belongs_to'設置爲'默認TRUE'以及它是如何與所需的標誌我的問題。我敢打賭,人們會爲此感恩。我不再在這個項目上工作,所以不能很容易地測試 –