LoginSignup
53
67

More than 5 years have passed since last update.

railsで複数画像を一度にアップロードする方法

Last updated at Posted at 2016-05-04

railsのプロジェクトで複数ファイルを一度にアップロードしたい時があります!

そんなときにはこんな感じでやればうまくいく。という一例をまとめておきます。

①まずアップロードに使うviewを編集します

といっても、
:multiple => trueをつければOK!

_form.html.slim
= form_for @photo do |f|
  - if @photo.errors.any?
    #error_explanation
      h2 = "#{pluralize(@photo.errors.count, "error")} prohibited this photo from being saved:"
      ul
        - @photo.errors.full_messages.each do |message|
          li = message

  .field.mb_20
    = f.label :content
    = f.file_field :content, :multiple => true
  .field.mb_20
    = f.text_field :title
  .actions = f.submit

ちなみ今回は、:titleも一緒にアップロードすることにします

②次にControllerのStrong Parametersをmultipleに対応させます

hashっぽく{}でくくって、配列が入る事を指定します

controller.rb

def photo_params
  params.require(:photo).permit( :title, {content: []})
end

これで、photo_paramsでcontentの配列までとれるはずです。

③次にModelにメソッドを用意します

photo.rb

def self.create_photos_by(photo_params)

/* そもそも一枚も上がってきてない時のためのvalidate */
  return false if photo_params[:content].nil?

/* 途中でエラった時にRollbackするようにTransaction */
  Photo.transaction do 

/* アップロードされた画像を一枚ずつ処理 */
    photo_params[:content].each do |photo|
      new_photo = Photo.new(title: photo_params[:title], content: photo)
      return false unless new_photo.save!
    end
  end

  true
end

④じゃあ、作ったModelメソッドにparameterをぶん投げます

controller.rb

def create
  @photo = Photo.new(photo_params) /* ここはformatの部分で使うインスタンス変数用 */

  respond_to do |format|
    if Photo.create_photos_by(photo_params)
      format.html { redirect_to photos_path, notice: 'Photo was successfully created.' }
      format.json { render :show, status: :created, location: @photo }
    else
      format.html { render :new }
      format.json { render json: @photo.errors, status: :unprocessable_entity }
    end
  end
end

これで完成!

53
67
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
67