いつも大変お世話になっておりますm(_ _)m
Railsを使って、複数画像をアップロードできるようにしたいのですが、
どうしてもエラーが発生して手こずってしまっています。。
DBに保存はされているみたいなのですが、imageカラムがすべて「NULL」となってしまっています。
開発環境
- ruby on rails 5.0.0.1
- carrierwave
エラー
Ruby
1 SQL (0.6ms) INSERT INTO "thumbnail" ("protype_id", "created_at", "updated_at") VALUES (?, ?, ?) [["protype_id", 45], ["created_at", 2017-09-10 07:47:04 UTC], ["updated_at", 2017-09-10 07:47:04 UTC]] 2 SQL (0.4ms) INSERT INTO "thumbnail" ("protype_id", "created_at", "updated_at") VALUES (?, ?, ?) [["protype_id", 45], ["created_at", 2017-09-10 07:47:04 UTC], ["updated_at", 2017-09-10 07:47:04 UTC]] 3 SQL (0.1ms) INSERT INTO "thumbnail" ("protype_id", "created_at", "updated_at") VALUES (?, ?, ?) [["protype_id", 45], ["created_at", 2017-09-10 07:47:04 UTC], ["updated_at", 2017-09-10 07:47:04 UTC]] 4 SQL (0.1ms) INSERT INTO "thumbnail" ("protype_id", "created_at", "updated_at") VALUES (?, ?, ?) [["protype_id", 45], ["created_at", 2017-09-10 07:47:04 UTC], ["updated_at", 2017-09-10 07:47:04 UTC]] 5 SQL (0.1ms) INSERT INTO "thumbnail" ("protype_id", "created_at", "updated_at") VALUES (?, ?, ?) [["protype_id", 45], ["created_at", 2017-09-10 07:47:04 UTC], ["updated_at", 2017-09-10 07:47:04 UTC]] 6 SQL (0.1ms) INSERT INTO "thumbnail" ("protype_id", "created_at", "updated_at") VALUES (?, ?, ?) [["protype_id", 45], ["created_at", 2017-09-10 07:47:04 UTC], ["updated_at", 2017-09-10 07:47:04 UTC]]
構造
こちらのQiita記事を参考にしておりまして、Protypeモデルにthumbnailモデル(写真)がネスト化して紐づいています。
ソースコード
- ビュー(new.html.erb)
ruby
1 <%= form_for(@post, :multipart => true) do |f| %> 2 <label for="name">名前</label> 3 <%= f.name_field :name %> 4 <label for="textarea">画像を投稿</label> 5 <%= f.fields_for :thumbnails do |a| %> 6 <%= a.file_field :image, class:"form-control form-none", multiple: "true", name: "protype" %> 7 <% end %> 8 <% end %>
- コントローラー(protypes_controller.rb)
ruby
1 def new 2 if signed_in? 3 @post = current_user.thumbnails.build 4 6.times { @post.thumbnails.build } 5 else 6 render 'new' 7 end 8 end 9 10 def create 11 @protype = current_user.protypes.create(create_params) 12 6.times { @protype.thumbnails.build } 13 if @protype.save 14 redirect_to root_path 15 else 16 render 'new' 17 end 18 end 19 20 private 21 22def create_params 23 params.require(:protype).permit(:id, :name, thumbnails_attributes: [:id, :protype_id, :image]) 24 end
追記(モデル)
- モデル(protype.rb)
Ruby
1class Protype < ApplicationRecord 2 has_many :thumbnails 3 accepts_nested_attributes_for :thumbnails, :allow_destroy => true 4end
- モデル(thumbnail.rb)
Ruby
1class Thumbnail < ApplicationRecord 2 belongs_to :protype 3 mount_uploader :image, ThumbnailUploader 4end
こちらの質問者さんが質問されているように、multipartがないのが問題かと思ったのですが、そういうわけでなさそうで・・・
すみませんが、どなたかご教示くださいませm(_ _)m
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/11 09:25