【发布时间】:2015-10-03 05:51:57
【问题描述】:
已编辑:我在最后添加了解决方案。
我完全按照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"
知道问题出在哪里吗?
提前致谢!
已编辑:解决方案是将 serialize :images 添加到产品模型中。很简单!感谢来自 CarrierWave github 的 @johnmcl。
【问题讨论】:
-
Product.find(...).images.inspect返回什么? -
@TimoSchilling,我添加了您问题的答案。
-
@GlauberSantana 你能把你的最终代码发布在一个要点中吗?
标签: mysql ruby-on-rails activeadmin carrierwave