试试这个。
在这里
avatar
是图像字段名。我认为您正确地运行了回形针命令来生成图像模型。
#Product Controller
if params[:images]
params[:images].each { |image|
@product.create_image(avatar: image, user_id: current_user.id)
}
end
类型
<%= file_field_tag "images[]", type: :file,:required => true %>
belongs_to :product
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png",
size: { in: 0..1000.kilobytes },
url: "/system/:hash.:extension",
hash_secret: "abc123"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
validates_attachment :avatar, :presence => true,
:size => { :in => 0..5000.kilobytes}
产品型号
has_many :images, dependent: :destroy
[#<ActionDispatch::Http::UploadedFile:0x007f90b22c5340 @tempfile=#<Tempfile:/tmp/RackMultipart20170929-7646-eqf009.jpeg>, @original_filename="job_post.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book_image[]\"; filename=\"job_post.jpeg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f90b22c52f0 @tempfile=#<Tempfile:/tmp/RackMultipart20170929-7646-1qhrsy8.jpeg>, @original_filename="query.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"book_image[]\"; filename=\"query.jpeg\"\r\nContent-Type: image/jpeg\r\n">]