Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 带CarrierWave和多文件上载的ActiveAdmin_Mysql_Ruby On Rails_Activeadmin_Carrierwave - Fatal编程技术网

Mysql 带CarrierWave和多文件上载的ActiveAdmin

Mysql 带CarrierWave和多文件上载的ActiveAdmin,mysql,ruby-on-rails,activeadmin,carrierwave,Mysql,Ruby On Rails,Activeadmin,Carrierwave,编辑:我在最后添加了解决方案 我完全遵循了这个原则,多部分技巧帮助我成功地上传了多个图像。但我还是在图片展示上出错了 我使用的是Rails 4.2.1、ActiveAdmin 1.0.0.pre1和MySql 我的代码: # Gemfile # (...) gem 'carrierwave', github: 'carrierwaveuploader/carrierwave' gem "mini_magick" # (...) # migration class CreateProducts

编辑:我在最后添加了解决方案

我完全遵循了这个原则,多部分技巧帮助我成功地上传了多个图像。但我还是在图片展示上出错了

我使用的是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
似乎保存到数据库中的对象是一个单项目数组,与我在上传表单中选择的文件数量无关。 映像按预期保存在文件系统上

编辑:

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={}>}>]"
"/uploads/product/images/1/%5B%22image_1.jpg%22%2C%20%22image_2.png%22%5D"
知道问题出在哪里吗

提前谢谢


编辑:解决方案是将serialize:images添加到产品模型中。太简单了!多亏了CarrierWave github的@johnmcl。

看起来您没有正确调用图像版本(在本例中为拇指)。试试这个

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

我认为多文件上传仍处于测试阶段


当我想为一个模型创建多个图像时,我只需创建
modelmimage
model,并在它们之间建立
one-to-many
关系。

什么是
Product.find(…).images.inspect
returns?@TimoSchilling,我为你的问题添加了答案。@GlauberSantana你能在摘要中发布你的最终代码吗,我认为问题在于carrierwave在数据库中保存对象的方式(例如:[“image_1.jpg”、“image_2.jpg”)。你的补丁坏了。QQQ,我也这么认为。
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