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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

2083閲覧

Railsで掲示板作成中。複数のカテゴリを付与して、トピックスを作成したい。

chanMiho

総合スコア7

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

1グッド

3クリップ

投稿2020/03/28 08:45

編集2020/03/28 08:48

前提・実現したいこと

初学者です。Ruby on Rails5.2.4.2を使用し、こちらを参考にして掲示板を作成しております。
※参考元サイトには、「投稿」と「カテゴリ」を紐付けするのを説明していますが、
これを応用して、私は「スレッドトピックス」と「複数のカテゴリ」を紐付けしようとしています。

複数カテゴリが付与可能な状態で、スレッドトピックスの作成機能を実装中、
トップページを表示する際に、下記エラーにどハマりしてしまいました。
初歩的な問題かもしれませんが、ご教示いただけると嬉しいです。

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

ターミナル上でのエラー文

ActionView::Template::Error (undefined method `category_ids' for #<Topic::ActiveRecord_Relation:0x00007fd97c183b18>): 14: <%# 存在するカテゴリの数だけチェックボックスを作成 %> 15: <%= f.collection_check_boxes(:category_ids, Category.all, :id, :category_name) do |c| %> 16: <%= c.label do %> 17: <%= c.check_box + c.text%> 18: <% end %> 19: <% end %> 20: </p> app/views/topics/index.html.erb:17:in `block (3 levels) in _app_views_topics_index_html_erb__4163002811688555481_70286033294240' app/views/topics/index.html.erb:16:in `block (2 levels) in _app_views_topics_index_html_erb__4163002811688555481_70286033294240' app/views/topics/index.html.erb:15:in `block in _app_views_topics_index_html_erb__4163002811688555481_70286033294240' app/views/topics/index.html.erb:9:in `_app_views_topics_index_html_erb__4163002811688555481_70286033294240'

localhost:3000 ブラウザのエラー文

NoMethodError in Topics#index Showing /Users/name/workspace/originalApp3/app/views/topics/index.html.erb where line #17 raised: undefined method `category_ids' for #<Topic::ActiveRecord_Relation:0x00007fd97c183b18> Extracted source (around line #17): 15 <%= f.collection_check_boxes(:category_ids, Category.all, :id, :category_name) do |c| %> 16 <%= c.label do %> 17 <%= c.check_box + c.text%> 18 <% end %> 19 <% end %> 20 </p>

該当のソースコード

routes.rb

Rails.application.routes.draw do root 'topics#index' get 'topics/thread/:id' => 'topics#thread', as: :topics_show post 'topics/create' => 'topics#create' end

コントローラ

class TopicsController < ApplicationController def index @topics = Topic.all @newTopic = Topic.new end def thread end def create @topics = Topic.create(topic_params) redirect_to topics_index_path end def topic_params params.require(:topics).permit(:title, category_ids: []) end end

index.html.erb(トップページ)

<h1>トピック一覧</h1> <ul> <% @topics.each do |topic| %> <li><%= link_to topic.title, topics_show_path(topic.id) %></li> <% end %> </ul> <h1>トピック新規登録</h1> <%= form_with model: @topics, url: {controller: 'topics', action: 'create' } do |f| %> <%= f.label :title, 'トピックスタイトル' %> <%= f.text_field :title %> <p> <%= f.label :category, 'カテゴリ' %> <%# 存在するカテゴリの数だけチェックボックスを作成 %> <%= f.collection_check_boxes(:category_ids, Category.all, :id, :category_name) do |c| %> <%= c.label do %> <%= c.check_box + c.text%> <% end %> <% end %> </p> <%= f.submit 'トピックス作成'%> <% end %>

topicモデル

class Topic < ApplicationRecord has_many :topic_category_relations has_many :categories, through: :topic_category_relations end

categoryモデル

class Category < ApplicationRecord has_many :topic_category_relations has_many :topics, through: :topic_category_relations end

中間テーブルモデル

class TopicCategoryRelation < ApplicationRecord belongs_to :topic belongs_to :category end

schema.rb

create_table "categories", force: :cascade do |t| t.string "category_name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "topic_category_relations", force: :cascade do |t| t.integer "topic_id" t.integer "category_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "topics", force: :cascade do |t| t.string "title" t.datetime "created_at", null: false t.datetime "updated_at", null: false end

試したこと

・topicテーブルに値を1つ作成
・categoryテーブルに値を2つ作成
・collection_check_boxesの引数の見直し→恐らく合っていると思います。
・ローカルサーバー再起動

どうもどこに原因があるのか、なかなか見当がつかないです。。

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

Ruby: 2.5.0
Rails: 5.2.4.2
OS: MacOS
IDE: VSCode

DrqYuto👍を押しています

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

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

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

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

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

guest

回答1

0

自己解決

自己解決しました。
コントローラのindexメソッドに、@topicsというインスタンス名に修正。
(viewのform_withで挿入していたモデル名と一致させることによって、method定義がされていると認識されて解決。)

 def index @topicsAll = Topic.all @topics = Topic.new @topics.topic_category_relations.build end def thread end def create @topics = Topic.new(topic_params) @topics.save redirect_to topics_index_path end def topic_params params.require(:topics).permit(:title, {:category_ids => []}) end

投稿2020/03/29 00:04

chanMiho

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問