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命令
生成了ProductUploaderclass 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
上传图像后,图像以params显示。但是没有保存在表格中。这是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。我需要在创建代码中更改它,但我没有正确访问params
答案 0 :(得分:1)
让我们将你的问题分成子问题:
您的数据库和控制器正常运行。无需改变任何东西!
您上传的图片(在参数[:image]中)是String
,而不是ActionDispatch::Http::UploadedFile
。您应该将multipart form data添加到表单中。
您可以使用params[:image].original_filename
访问上传的文件名。如果要更改文件名,则可以重新定义CarrierWave处理程序的filename
方法(最后一个,已注释)。