我有一个带有图像属性的 Artwork 模型,其中安装了 Carrierwave。在编写控制器规范时,我意识到图像字段保持空白,即使我认为我正在传递一个文件对象。
我的调试信息告诉我,问题出在我安装了 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"]
请问有什么想法吗?是否有我忘记设置的控制器参数?