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

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

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

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

652閲覧

タグ付け機能が実装できない

taco

総合スコア4

Ruby

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/07/21 05:50

前提・実現したいこと

(例)ruby(rails)でタグ付け機能を作っています。
な機能を実装中に以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

エラーメッセージ ActiveModel::UnknownAttributeError in ClassCommunicationsController#create unknown attribute 'class_communication_day(1i)' for ClassCommunicationsTag.

該当のソースコード

models

1class ClassCommunicationsTag 2 3 include ActiveModel::Model 4 attr_accessor :class_communication_day, :title, :text, :tag_name, :images 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) 15 16 tag = Tag.where(tag_name: tag_name).first_or_initialize 17 tag.save 18 19 20 TagClassCommunication.create(class_communication_id: class_communication.id, tag_id: tag.id) 21 end 22 23end

controller

1lass 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.save 14 redirect_to class_communications_path 15 else 16 render :new 17 end 18 end 19 20 def show 21 @class_communication = ClassCommunication.find(params[:id]) 22 @class_communications = ClassCommunication.all 23 end 24 25 def edit 26 @class_communication = ClassCommunication.find(params[:id]) 27 end 28 29 def update 30 @class_communication = ClassCommunication.find(params[:id]) 31 @class_communication.update(class_communication_params) 32 if @class_communication.save 33 redirect_to class_communications_path 34 else 35 render :edit 36 end 37 end 38 39 def destroy 40 @class_communication = ClassCommunication.find(params[:id]) 41 @class_communication.destroy 42 redirect_to class_communications_path 43 end 44 45 private 46 47 def class_communication_params 48 params.require(:class_communications_tag).permit(:class_communication_day, :title, :text, :tag_name, images: []).merge(user_id: current_user.id, class_communication_id: params[:id]) 49 end 50end 51

view

1<h1>学級通信作成</h1> 2<%= form_with model: @class_communication, url: class_communications_path, class: 'registration-main', local: true do |f| %> 3 4 <div class='form-wrap'> 5 <div class='form-header'> 6 <h1 class='form-header-text'> 7 学級通信 8 </h1> 9 </div> 10 11 <%= render "error_messages", resource: @class_communication %> 12 13 <div class="form-group"> 14 <div class='form-text-wrap'> 15 <label class="form-text">書いた日</label> 16 <span class="indispensable">必須</span> 17 </div> 18 <div class='input-birth-wrap'> 19 <%= raw sprintf( 20 f.date_select( 21 :class_communication_day, 22 class:'select-birth', 23 id:"birth-date", 24 use_month_numbers: true, 25 prompt:'--', 26 start_year: 2021, 27 end_year: (Time.now.year + 5), 28 date_separator: '%s'), 29 "<p> 年 </p>", "<p> 月 </p>") + "<p> 日 </p>" %> 30 </div> 31 </div> 32 33<div class="form-group"> 34 <div class='form-text-wrap'> 35 <label class="form-text">画像</label> 36 <span class="indispensable">必須</span> 37 </div> 38 <%= f.file_field :images, name: "class_communication[images][]", class:"profile_image", id:"profile_image" %> 39 </div> 40 <div id="image-list"></div> 41 42 43 <div class="form-group"> 44 <div class='form-text-wrap'> 45 <label class="form-text">タイトル</label> 46 <span class="indispensable">必須</span> 47 </div> 48 <%= f.text_area :title, class:"input-default", id:"nickname", placeholder:"例) 楽ちょ太郎", maxlength:"40" %> 49 </div> 50 51 52 <div class="form-group"> 53 <div class='form-text-wrap'> 54 <label class="form-text">学級通信</label> 55 <span class="indispensable">必須</span> 56 </div> 57 <%= f.text_area :text, class:"input-introduction", id:"introduction", placeholder:"例)楽ちょ太郎です。みんなと仲良く、力を合わせていきます!よろしく!" %> 58 </div> 59 60 <div class="form-group"> 61 <div class='form-text-wrap'> 62 <label class="form-text">タグ付け</label> 63 <span class="indispensable">必須</span> 64 </div> 65 <%= f.text_field :tag_name, class:"input-introduction", id:"introduction", placeholder:"例)楽ちょ太郎です。みんなと仲良く、力を合わせていきます!よろしく!" %> 66 </div> 67 68 69 70<div class='register-btn'> 71 <%= f.submit "登録" ,class:"register-yellow-btn" %> 72 </div> 73 <%= link_to "トップページへ", root_path, class: 'register-info' %> 74 <%= link_to "ログイン画面へ", new_user_session_path, class: 'register-info' %> 75</div> 76<% end %> 77 78 79

試したこと

new.htmlからコントローラーへdateが送られていないことが原因ではないかと考えました。binding.pryでparamsを確認すると日付は存在しました。ただ登録ができません。初学者でコードは読みにくいところばかりで申し訳ありませんが教えてください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

自己解決

include ActiveRecord::AttributeAssignmentをmodelに記入するとうまくいきました。

投稿2021/07/21 20:21

taco

総合スコア4

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問