abab7200 score 118
2017/09/21 08:55 投稿
【Rails4】Paperclipを利用した登録フォームで、1回で画像を複数uploadしたい |
Paperclipで画像を複数投稿できるフォームを設定したいのですが、 |
multiple: trueを設定すると、controller側で値がNillになり、DBでもデータ登録ができておりません。 どなたかフォーム上で複数uploadが可能になる方法をご教授いただければと存じます |
バージョン |
Rails4 |
Ruby2.1 |
Picture model |
```ここに言語を入力 |
class Picture < ActiveRecord::Base |
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" } |
validates_attachment :avatar, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] } |
end |
``` |
Picture controller |
```ここに言語を入力 |
class PicturesController < ApplicationController |
def index |
@pictures = Picture.all |
end |
def new |
@picture = Picture.new |
end |
def create |
@picture = Picture.new(picture_params) |
byebug |
if @picture.save |
redirect_to dashboards_path |
else |
render 'new' |
end |
end |
private |
def picture_params |
params.require(:picture).permit(:name, :photo, :avatar) |
end |
end |
``` |
Picture new.html.slim |
```ここに言語を入力 |
= simple_form_for @picture, url: pictures_path do |form| |
= form.file_field :avatar, as: :file , multiple: true |
= form.submit "送信" |
``` |
byebugした際のcreate処理で渡されるパラメーター |
```ここに言語を入力 |
11: def create |
12: @picture = Picture.new(picture_params) |
13: byebug |
=> 14: if @picture.save |
15: redirect_to dashboards_path |
16: else |
17: render 'new' |
18: end |
(byebug) |
(byebug) @picture |
#<Picture id: nil, name: nil, string: nil, created_at: nil, updated_at: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil> |
``` |
``` |