编辑:我最后添加了解决方案。
我完全按照alestuber's instructions,多部分技巧帮助我成功上传了多张图片。但是我仍然会在图像展示上出错。
我使用的是Rails 4.2.1,ActiveAdmin 1.0.0.pre1和MySql。
我的代码:
# Gemfile
# (...)
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
gem "mini_magick"
# (...)
# migration
class CreateProducts < ActiveRecord::Migration
def up
create_table :products do |t|
t.string :name
t.string :slug, null: false
t.decimal :price, precision: 8, scale: 2
t.text :description
t.text :images, array: true
t.references :category
t.timestamps null: false
end
add_index :products, :slug, unique: true
Product.create_translation_table! :name => :string, :description => :text
end
def down
drop_table :products
Product.drop_translation_table!
end
end
#app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fit => [200, 200]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
#app/models/product.rb
class Product < ActiveRecord::Base
belongs_to :category
translates :name, :description
active_admin_translates :name, :description
extend FriendlyId
friendly_id :name, use: :slugged
mount_uploaders :images, ImageUploader
def to_s
name
end
end
#app/admin/product.rb
ActiveAdmin.register Product do
permit_params :category_id, :price, translations_attributes: [:id, :locale, :name, :description, :_destroy], images: []
index do
selectable_column
column :name
column :category
column :price do |product|
number_to_currency product.price
end
translation_status_flags
actions
end
show do
attributes_table do
row :slug
translated_row :name
translated_row :description
row :category
row :price do |product|
number_to_currency product.price
end
row :images do
ul do
product.images.each do |image|
li do
image_tag(image.url(:thumb))
end
end
end
end
end
active_admin_comments
end
form html: { multipart: true } do |f|
f.semantic_errors
f.inputs do
f.translated_inputs auto_sort: false do |t|
t.input :name
t.input :description
end
end
f.inputs do
f.input :category
f.input :price
f.input :images, as: :file, input_html: { multiple: true }
end
actions
end
end
错误:
Started GET "/uploads/product/images/1/thumb_%5B%22image_1.jpg%22%2C%20%22image_2.png%22%5D" for 10.0.2.2 at 2015-07-14 13:01:43 +0000
ActionController::RoutingError (No route matches [GET] "/uploads/product/images/1/thumb_%5B%22image_1.jpg%22%2C%20%22image_2.png%22%5D")
保存到数据库中的对象似乎是一个项目数组,与我在上传表单中选择的文件数量无关。 图像将按预期保存在文件系统中。
编辑:
Product.find(...).images.inspect
返回:
"[#<ImageUploader:0xd4afc24 @model=#<Product id: 1, name: \"Camiseta\", slug: \"camiseta\", price: #<BigDecimal:d4ac010,'0.599E2',18(18)>, description: \"\", images: \"[\\\"image_1.jpg\\\", \\\"image_2.png\\\"]\", category_id: 1, created_at: \"2015-07-14 13:01:27\", updated_at: \"2015-07-14 16:41:36\">, @mounted_as=:images, @storage=#<CarrierWave::Storage::File:0xd4afbe8 @uploader=#<ImageUploader:0xd4afc24 ...>>, @file=#<CarrierWave::SanitizedFile:0xd4af904 @file=\"/vagrant/public/uploads/product/images/1/[\\\"image_1.jpg\\\", \\\"image_2.png\\\"]\", @original_filename=nil, @content_type=nil>, @versions={:thumb=>#<ImageUploader::Uploader94172950:0xd4af8dc @model=#<Product id: 1, name: \"Camiseta\", slug: \"camiseta\", price: #<BigDecimal:d4ac010,'0.599E2',18(18)>, description: \"\", images: \"[\\\"image_1.jpg\\\", \\\"image_2.png\\\"]\", category_id: 1, created_at: \"2015-07-14 13:01:27\", updated_at: \"2015-07-14 16:41:36\">, @mounted_as=:images, @parent_version=#<ImageUploader:0xd4afc24 ...>, @storage=#<CarrierWave::Storage::File:0xd4af8a0 @uploader=#<ImageUploader::Uploader94172950:0xd4af8dc ...>>, @file=#<CarrierWave::SanitizedFile:0xd4af580 @file=\"/vagrant/public/uploads/product/images/1/thumb_[\\\"image_1.jpg\\\", \\\"image_2.png\\\"]\", @original_filename=nil, @content_type=nil>, @versions={}>}>]"
Product.find(...).images.first.url
返回:
"/uploads/product/images/1/%5B%22image_1.jpg%22%2C%20%22image_2.png%22%5D"
知道问题在哪里?
提前致谢!
EDITED:解决方案是将serialize:images添加到产品模型中。很简单!感谢来自CarrierWave github的@johnmcl。
答案 0 :(得分:2)
我认为多个文件上传仍处于测试阶段。
当我想为一个模型创建多个图像时,我只需创建ModelImage
模型并在它们之间建立one-to-many
关系。
答案 1 :(得分:1)
看起来你没有正确调用图像版本(在这种情况下是拇指)。试试这个......
show do
attributes_table do
row :slug
translated_row :name
translated_row :description
row :category
row :price do |product|
number_to_currency product.price
end
row :images do
ul do
product.images.each do |image|
li do
image_tag(image.thumb.url)
end
end
end
end
end