Rails使用Carrierwave上传多个图像

时间:2017-07-07 13:41:54

标签: ruby-on-rails ruby carrierwave

我一直关注此tutorial上传多张载波图片。

它按预期工作,但它在图像下方显示如下:

[#<ImageUploader:0xaeb3490 @model=#<Post id: 19, created_at: "2017-07-07 09:33:50", updated_at: "2017-07-07 09:33:50", lat: 54.635, long: -5.84582, images: ["87a8c63a9be6af171244299fc0b0a27e--stunning-art-street-art-illusions.jpg"], artist: "Vemeer", year: nil>, @mounted_as=:images, @storage=#<CarrierWave::Storage::File:0xaeb3430 @uploader=#<ImageUploader:0xaeb3490 ...>>, @file=#<CarrierWave::SanitizedFile:0xaeb3100 @file="C:/path/public/uploads/post/images/19/87a8c63a9be6af171244299fc0b0a27e--stunning-art-street-art-illusions.jpg", @original_filename=nil, @content_type=nil>, @versions={}>]

正如在获得多次上传的教程中所解释的,我做了以下内容:

Rails迁移以创建图像数组:

class AddImagesToGallery < ActiveRecord::Migration
  def change
    add_column :posts, :images, :string, array: true, default: [] 
  end
end

已编辑的帖子/ _form.html.haml:

= f.file_field :images, multiple: true

已编辑post_params

def post_params
    post_params.require(:post).permit(:artist, :year, :long, :lat, {images: []})
end

在posts / show.html.haml

中显示图片
=@mural.images.each do |image|
    = image_tag(image.url)

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

  

它按预期工作,但它在图像下方显示如下:

[#<ImageUploader:0xaeb3490 @model=#<Post id: 19, created_at: "2017-07-07 09:33:50", updated_at: "2017-07-07 09:33:50", lat: 54.635, long: -5.84582, images: ["87a8c63a9be6af171244299fc0b0a27e--stunning-art-street-art-illusions.jpg"], artist: "Vemeer", year: nil>, @mounted_as=:images, @storage=#<CarrierWave::Storage::File:0xaeb3430 @uploader=#<ImageUploader:0xaeb3490 ...>>, @file=#<CarrierWave::SanitizedFile:0xaeb3100 @file="C:/path/public/uploads/post/images/19/87a8c63a9be6af171244299fc0b0a27e--stunning-art-street-art-illusions.jpg", @original_filename=nil, @content_type=nil>, @versions={}>]

这一行

=@mural.images.each do |image|
    = image_tag(image.url)

应该是

- @mural.images.each do |image|
    = image_tag(image.url)

注意=-之间的区别。 =用于显示输出。 -用于迭代对象。