我有一個Artwork模型,其中載有Carrierwave的圖片屬性。在編寫控制器規範時,我意識到圖像字段保持空白,即使當我認爲我傳入文件對象時也是如此。我需要在控制器中傳遞哪些參數到Carrierwave-mounted模型?
我的調試信息告訴我,問題出在我載有CarrierWave的圖像屬性上,而不是別的。我不認爲我傳遞了它需要的信息,但我不知道該怎麼嘗試。
這裏的控制器動作,以及一些調試信息,以確定問題:
def create
@artwork = @imageable.artworks.new(params[:artwork])
logger.debug "Artwork should be valid: #{@artwork.valid?}"
logger.debug "Errors: #{@artwork.errors.full_messages}"
if @artwork.save
flash[:success] = "Artwork created."
redirect_to [@imageable, :artworks]
else
flash[:error] = "Artwork not created."
render :new
end
end
在測試日誌我看到這個運行我的規格後:
Processing by ArtworksController#create as HTML
Parameters: {"artwork"=>{"image"=>"#<File:0x4515480>", "title"=>"Portrait","year"=>"2012", "surface_type"=>"canvas", "size"=>"10 x 10", "price"=>"100.0", "for_sale"=>false, "prints_available"=>false, "notes"=>"extra notes"}, "file"=>"#<File:0x46c6938>", "originals_gallery_id"=>"1"}
[1m[36mOriginalsGallery Load (0.0ms)[0m [1mSELECT "originals_galleries".* FROM "originals_galleries" WHERE "originals_galleries"."id" = ? LIMIT 1[0m [["id", "1"]]
Artwork should be valid: false
Errors: ["Image can't be blank"]
有什麼想法嗎?有沒有我忘記設置的控制器參數?
卡住試圖解決同樣的問題。太感謝了! – wyclin