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

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

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

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

Q&A

解決済

1回答

528閲覧

テーブルに画像が保存できない

taco

総合スコア4

Ruby

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

0グッド

1クリップ

投稿2021/07/22 21:38

編集2021/07/22 21:51

解決したいこと

railsで簡単な日記を投稿できるシステムを作っています。
一つのページから2つのテーブル(tagsテーブル、class_communicationsテーブル)に保存できるように実装中です。
また、画像はactive_storageを使って、複数枚保存できるようにしています。
データを投稿すると、次のようなエラーが出ます。

イメージ説明

複数枚の画像を投稿できるように実装していましたが、タグ付け機能を実装するため、次のようにコードを記述したところ、エラーが出るようになりました。

model

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

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 if @class_communication.valid? 14 @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中略 27 28 private 29 30 def class_communication_params 31 params.require(:class_communications_tag).permit(:class_communication_day, :title, :text, :tag_name, images: []).merge(user_id: current_user.id) 32 end 33end 34

view

1<%= form_with model: @class_communication, url: class_communications_path, class: 'registration-main', local: true do |f| %> 2 3中略 4 5div class="form-group"> 6 <div class='form-text-wrap'> 7 <label class="form-text">画像</label> 8 <span class="indispensable">必須</span> 9 </div> 10 <%= f.file_field :images, name: "class_communication[images][]", class:"profile_image", id:"profile_image" %> 11 </div> 12 <div id="image-list"></div> 13 14中略 15 16<% end %>

試したこと

imagesのデータが送られていないことが問題と思い、binding.pryを用い検証しました。画像は次の通りです。
イメージ説明
なんとか自力解決を目指していますが、うまくいきません。もしもヒントをいただける方がいらっしゃったらよろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

class_communicationsのモデルはどうなっていますか?

投稿2021/07/25 13:16

J_O

総合スコア143

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

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

taco

2021/07/26 07:14

返信ありがとうございます。すみません。 一旦、タグ付け機能を諦めました!!時間があったらまたトライしたいです。 一応モデルは class ClassCommunication < ApplicationRecord belongs_to :user has_many_attached :images has_many :likes, dependent: :destroy has_many :tag_class_communications, dependent: :destroy has_many :tags, through: :tag_class_communications end でした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問