【发布时间】:2016-07-04 18:56:04
【问题描述】:
def create
@product = Product.find(1)
@product.images << params[:image]
@product.save
end
架构是
t.string "images", default: [], array: true
如果图像已经存在,我希望将参数中的图像分配给图像数组。新图像应该附加,否则它应该是第一个
这是产品型号
class Product < ActiveRecord::Base
mount_uploader :images, ProductUploader
validates :name, presence: true
validates :price, presence: true
validates :availability, presence: true
validates :about, presence: true
validates :ref, presence: true
validates :texture, presence: true
validates :shipping, presence: true
validates :category, presence: true
validates :notes, presence: true
end
我通过carrierwave uploader命令生成了ProductUploader
class ProductUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
上传图片后,图片显示在参数中。但不会保存在表中。这是byebug的截图
编辑:
我已经取得了一些进展
对于这样的图像,我将字符串 [] 更新为 json
文件不是像字符串那样出现,而是作为文件出现。现在唯一的问题是,我无法在参数中正确访问名称name=\"image[avatar_1]\"
"utf8"=>"✓", "authenticity_token"=>"JbtcVdUvPJIzSHycX0wdr349zcATDN51vSewI33nvHIKQdhxxgxWUEJz9ZsubNrgvq2ftO+DosHXWM8Tipce7w==", "image"=>{"avatar_1"=>#<ActionDispatch::Http::UploadedFile:0x007f2ba8377f78 @tempfile=#<Tempfile:/tmp/RackMultipart20160704-20497-1czlw3s.png>, @original_filename="no-save.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image[avatar_1]\"; filename=\"no-save.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"submit", "controller"=>"upload", "action"=>"create"}
这是视图文件
<%= simple_form_for :image, method: :post, url: save_image_path do |f| %>
<%= f.file_field :avatar_1 %>
<%= f.submit :submit %>
<% end %>
这里是 avatar_1。我需要在创建代码中更改它,但我没有正确访问参数本身
【问题讨论】:
-
您使用的是哪个数据库引擎?错误看起来如何?此外,对于数组,您应该使用
text而不是string。 -
params[:image]包含String,而不是ActionDispatch::Http::UploadedFile。您可能应该将multipart form data 添加到您的表单中。然后再次。您使用的是哪个数据库引擎? PostgreSQL? mysql?最后,您应该检查this。 -
@Wikiti 请看一下编辑。根据您的输入,我取得了一些进展,我正在使用 postgres
-
您可以使用
params[:image].original_filename访问文件名。如果您需要重命名文件,请在您的 CarrierWave 处理程序(最后一个,已注释)上定义filename方法。 -
请写一个答案,你们的cmets集体做出了答案:) @Wikiti