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

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

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

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

Q&A

解決済

1回答

1289閲覧

フォームオブジェクトにてタグを付けたい

souda-takeru

総合スコア4

Ruby

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

0グッド

0クリップ

投稿2020/11/25 10:55

イベント投稿にタグを付けたい ここに質問の内容を詳しく書いてください。 rubyでイベントの内容等を投稿できるアプリを作っています。タグ付け機能を実装するためフォームオブジェクトを使用しイベント情報とタグの情報を同時に保存しようとしましたが、タグのカラムが定義されていないエラーが発生しました。初投稿でわかりにくいところもあると思いますがよろしくお願いいたします waito0202@gmail.com■■な機能を実装中に以下のエラーメッセージが発生しました。 ### 発生している問題・エラーメッセージ ```ruby ```NoMethodError in EventsController#create undefined method `tag_name' for #<Event:0x00007f9598d364b0> Extracted source (around line #23): def save event = Event.create(name: name, explanation: explanation, facility_id: facility_id, scale_id: scale_id, category_id: category_id, volunteer: volunteer,user_id: user_id) tag = Tag.where(tag_name: tag_name).first_or_initialize tag.save

該当のソースコード

event_model

ruby

1class Event < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to :user 4 has_many_attached :images 5 belongs_to :facility 6 belongs_to :scale 7 belongs_to :category 8 has_many :event_tag_relations 9 has_many :tags, through: :event_tag_relations 10 with_options presence: true do 11 validates :name 12 validates :explanation 13 validates :facility_id 14 validates :scale_id 15 validates :category_id 16 validates :tag_name 17 end 18 with_options numericality: { other_than: 1 } do 19 validates :facility_id 20 validates :scale_id 21 validates :category_id 22 end 23end 24

events_controller.rd

ruby

1class EventsController < ApplicationController 2 def index 3 @events = Event.all 4 end 5 6 def new 7 @event = EventsTag.new 8 end 9 10 def create 11 12 @event = EventsTag.new(event_params) 13 if @event.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 private 21 22 def event_params 23 params.require(:events_tag).permit(:name, :explanation, :facility_id, :scale_id, :category_id, :volunteer,:tag_name, images: []).merge(user_id: current_user.id) 24 end 25end 26 27 28```events_tag.rb

ruby

1class EventsTag 2 3 include ActiveModel::Model 4 attr_accessor :name, :explanation, :facility_id, :scale_id, :category_id, :volunteer, :tag_name, :user_id 5 6 7 with_options presence: true do 8 validates :name 9 validates :explanation 10 validates :facility_id 11 validates :scale_id 12 validates :category_id 13 validates :tag_name 14 end 15 with_options numericality: { other_than: 1 } do 16 validates :facility_id 17 validates :scale_id 18 validates :category_id 19 end 20 21 22 def save 23 event = Event.create(name: name, explanation: explanation, facility_id: facility_id, scale_id: scale_id, category_id: category_id, volunteer: volunteer,user_id: user_id) 24 tag = Tag.where(tag_name: tag_name).first_or_initialize 25 tag.save 26 27 EventTagRelation.create(event_id: event.id, tag_id: tag.id) 28 end 29end

_form.html.erb

ruby

1<div class="tag-field", id='tag-field'> 2 <%= f.label :tag_name, "タグ" %> 3 <%= f.text_field :tag_name, class:"input-tag" %> 4 </div> 5
### 試したこと binding.pryでparamsを確認しましたがtag_nameのデータは作られていました。 フォームオブジェクトについてのエラー情報を色々と探してみましたが見つかりませんでした ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ruby '2.6.5' ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

event.rb の中のvalidates :tag_nameを削除してみてください
おそらく、Eventにはtag_nameというカラムがないのにvalidatesで指定しているため
undefined method と言われてます。

投稿2020/11/26 03:50

neko_daisuki

総合スコア2090

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

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

souda-takeru

2020/11/26 09:30

validatesのカラム名が間違っていました。修正し無事解決しました。ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問