質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

0回答

728閲覧

Activestorageに複数枚画像が投稿できず困っています。

taco

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2021/07/21 21:25

編集2021/07/22 05:10

前提

Ruby on railsでclass_communication_day、title、text、tag、imagesを投稿できるシステムを作っています。
Formオブジェクトを使い、一つのページから2つのテーブル(tags、class_communications)に保存できるように頑張っているところです。また、画像はactive_storageを使って、複数枚保存できるようにしています。
データを送信するとimage以外のデータはテーブルに保存されますが、imagesのみ保存そのものができていません。
エラーメッセージは出ていません。
わかる方教えてください、

該当のソースコード

model

1class ClassCommunication < ApplicationRecord 2 belongs_to :user 3 has_many_attached :images 4 has_many :likes 5 has_many :tag_class_communications 6 has_many :tags, through: :tag_class_communications 7end

model

1class ClassCommunicationsTag 2 include ActiveRecord::AttributeAssignment 3 include ActiveModel::Model 4 attr_accessor :class_communication_day, :title, :text, :tag_name, :images, :user_id 5 with_options presence: true do 6 validates :class_communication_day 7 validates :title 8 validates :text 9 validates :images 10 validates :tag_name 11 end 12 13 def save 14 class_communication = ClassCommunication.create(class_communication_day: class_communication_day, title: title, text: text, images: images, user_id: user_id) 15 16 tag = Tag.where(tag_name: tag_name).first_or_initialize 17 tag.save 18 19 TagClassCommunication.create(class_communication_id: class_communication.id, tag_id: tag.id) 20 end 21 22end

controller

1class ClassCommunicationsController < ApplicationController 2 3 def index 4 @class_communications = ClassCommunication.all 5 end 6 7 def new 8 @class_communication = ClassCommunicationsTag.new 9 end 10 11 def create 12 @class_communication = ClassCommunicationsTag.new(class_communication_params) 13 binding.pry 14 if @class_communication.save 15 redirect_to class_communications_path 16 else 17 render :new 18 end 19 end 20 21 def show 22 @class_communication = ClassCommunication.find(params[:id]) 23 @class_communications = ClassCommunication.all 24 end 25 26 def edit 27 @class_communication = ClassCommunication.find(params[:id]) 28 end 29 30 def update 31 @class_communication = ClassCommunication.find(params[:id]) 32 @class_communication.update(class_communication_params) 33 if @class_communication.save 34 redirect_to class_communications_path 35 else 36 render :edit 37 end 38 end 39 40 def destroy 41 @class_communication = ClassCommunication.find(params[:id]) 42 @class_communication.destroy 43 redirect_to class_communications_path 44 end 45 46 private 47 48 def class_communication_params 49 params.require(:class_communications_tag).permit(:class_communication_day, :title, :text, :tag_name, images: []).merge(user_id: current_user.id) 50 end 51end 52

試したこと

binding.pryでどんなparamsが送られてきているかを確認していました。データ自体は送られてきているので、createに問題があるように思いました。その他わからない点があればアップします!

https://gyazo.com/e8cc0a1681be7b098500da390657ca08

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問