【问题标题】:upload multiple images in carrierwave rails在载波轨道中上传多个图像
【发布时间】:2015-02-21 10:18:12
【问题描述】:

我有一个 Event 模型,例如 Event has_many :images, as: :imageable 和 Image 模型,例如 Image belongs_to :imageable, polymorphic: true。我正在使用carrierwave,并且能够轻松地将单个图像上传到事件。但所需的场景是为每个事件上传多个图像。上传多张图片时,我也会在参数中获取图片。我只是不知道如何使用这些参数和存储图像

Event.rb
class Event < ActiveRecord::Base
  has_many :images, as: :imageable
  mount_uploader :image, EventImageUploader



Image.rb

class Image < ActiveRecord::Base
    belongs_to :imageable, polymorphic: true



Parameters in console on multiple input



    Parameters: {"utf8"=>"✓", "authenticity_token"=>"h75pCP+/S9rt16aFMXAagtaJq5msxWBUt9wfYzSinB8=", "event"=>{"title"=>"sfdf", "starts_at"=>"2014-12-10 14:47", "ends_at"=>"2014-12-17 14:47", "url"=>"https://softwaysolutions.atlassian.net/wiki/display/RADIO/2014/10/31/Radio+App+Initial+DB+Design", "description"=>"fssdfdfsf",
 "image"=>[#<ActionDispatch::Http::UploadedFile:0x0000000a5e93e0 @tempfile=#<Tempfile:/tmp/RackMultipart20141223-12927-1izxog6>, @original_filename="a1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"event[image][]\"; filename=\"a1.jpg\"\r\nContent-Type: image/jpeg\r\n">,
#<ActionDispatch::Http::UploadedFile:0x0000000a5e93b8 @tempfile=#<Tempfile:/tmp/RackMultipart20141223-12927-vs0l0e>, @original_filename="a7.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"event[image][]\"; filename=\"a7.jpg\"\r\nContent-Type: image/jpeg\r\n">,
#<ActionDispatch::Http::UploadedFile:0x0000000a5e9390 @tempfile=#<Tempfile:/tmp/RackMultipart20141223-12927-1q3vr93>, @original_filename="a8.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"event[image][]\"; filename=\"a8.jpg\"\r\nContent-Type: image/jpeg\r\n">, 
#<ActionDispatch::Http::UploadedFile:0x0000000a5e9368 @tempfile=#<Tempfile:/tmp/RackMultipart20141223-12927-1ccn4dk>, @original_filename="angular_logo.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"event[image][]\"; filename=\"angular_logo.png\"\r\nContent-Type: image/png\r\n">,
#<ActionDispatch::Http::UploadedFile:0x0000000a5e9340 @tempfile=#<Tempfile:/tmp/RackMultipart20141223-12927-ur48if>, @original_filename="email_2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"event[image][]\"; filename=\"email_2.jpg\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Create Event"}

【问题讨论】:

    标签: ruby-on-rails image time upload carrierwave


    【解决方案1】:

    一次上传多张图片可能很耗时,但您可以使用任何 jquery 插件,例如 uploadify,它会为每次上传发送一个 AJAX 请求。这样您还可以监控哪个图像上传失败或成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 2016-12-17
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 2017-04-03
      相关资源
      最近更新 更多