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

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

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

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

Ruby on Rails

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

パラメータ

関数やプログラム実行時に与える設定値をパラメータと呼びます。

Q&A

1回答

589閲覧

複数のモデルからの情報を出力できるようにしたい

isd_kisk

総合スコア15

Ruby

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

Ruby on Rails

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

パラメータ

関数やプログラム実行時に与える設定値をパラメータと呼びます。

0グッド

0クリップ

投稿2021/08/20 08:52

accepts_nested_attributes_forを使って、todoモデルとcategoryモデルを親子関係にして、それぞれのモデルの情報をindex.html.erbに表示したいのですが、表示されません。

コードは以下の通りです。

ruby

1categories_Controller.rb 2 3class CategoriesController < ApplicationController 4 def index 5 @categories = Category.all 6 @todos= Todo.all 7 end 8 9 def show 10 @category = Category.find(params[:id]) 11 end 12 13 def new 14 @category = Category.new 15 @todo = @category.Todos.new 16 end 17 18 def create 19 @category = Category.new(category_params) 20 if @category.save 21 flash[:notice] ="完了しました" 22 redirect_to categories_path 23 else 24 render "new" 25 end 26 end 27 28 def edit 29 @category = Category.find(params[:id]) 30 end 31 32 def update 33 @category = Category.find(params[:id]) 34 if @category.update(category_params) 35 flash[:notify] = "完了しました" 36 redirect_to category_path(@category) 37 else 38 render "index" 39 end 40 end 41 42 def destroy 43 @category = Category.find(params[:id]) 44 @category.destroy 45 if @category.destroy 46 flash[:message] = "完了しました" 47 redirect_to categories_path 48 end 49 end 50 51 private 52 def category_params 53 params.require(:categories).permit(:category,todos_attributes: [:body, :deadline]) 54 end 55end 56

ruby

1index.html.erb 2 3<table> 4 <thead> 5 <tr> 6 <th>deadline</th> 7 <th>task</th> 8 <th>status</th> 9 </tr> 10 <tr> 11 <%= form_with model:@category, local:true do |f| %> 12 <td><%= f.date_select :deadline %></td> 13 <td><%= f.text_area :body%></td> 14 <%= f.fields_for :categories do |c| %> 15 <td><%= c.select :category,[['重要度が高く緊急','urgent'],['重要度は高くないが、緊急','not important but urgent'],['重要度は高いが緊急じゃない','not urgent but important'],['重要度は高くなく、緊急ではない','not urgent and important']], include_blank: '選択してください' %></td> 16 <% end %> 17 <%= f.submit '送信' %> 18 <% end %> 19 </tr> 20 </thead> 21</table> 22 23<table> 24 <tbody> 25 <tr> 26 <% @categories.each do |category| %> 27 <% category.todos.each do |todo| %> 28 <td><%= @todo.body %></td> 29 <td><%= @todo.deadline%></td> 30 <td><%= @category.category %></td> 31 <% end %> 32 </tr> 33 <% end %> 34 </tbody> 35</table> 36

ruby

1todo.rb 2 3class Todo < ApplicationRecord 4 belongs_to :category 5end 6

ruby

1category.rb 2 3class Category < ApplicationRecord 4 has_many :todos, dependent: :destroy 5 accepts_nested_attributes_for :todos, allow_destroy: true 6end 7

rails

1schema.rb 2 3# This file is auto-generated from the current state of the database. Instead 4# of editing this file, please use the migrations feature of Active Record to 5# incrementally modify your database, and then regenerate this schema definition. 6# 7# This file is the source Rails uses to define your schema when running `bin/rails 8# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to 9# be faster and is potentially less error prone than running all of your 10# migrations from scratch. Old migrations may fail to apply correctly if those 11# migrations use external dependencies or application code. 12# 13# It's strongly recommended that you check this file into your version control system. 14 15ActiveRecord::Schema.define(version: 2021_08_18_114547) do 16 17 create_table "categories", force: :cascade do |t| 18 t.datetime "created_at", precision: 6, null: false 19 t.datetime "updated_at", precision: 6, null: false 20 t.string "category" 21 end 22 23 create_table "todos", force: :cascade do |t| 24 t.string "state" 25 t.datetime "deadline" 26 t.datetime "created_at", precision: 6, null: false 27 t.datetime "updated_at", precision: 6, null: false 28 t.string "body" 29 t.integer "category_id" 30 end 31 32end 33

※todosのstateは無視してもらって大丈夫です

また以下のように表示されているので、パラメータにデータは送られていると思うのですがどうなんでしょうか
イメージ説明

rails cを実行すると以下のような結果を得ることができます。

Todo.all
イメージ説明

Category.all
イメージ説明

category:nilになっているのが気になるのですが、categoryのデータが送られていないのでしょうか?

上手くできない要因としては、この2つが考えられるのですが、何が原因なのでしょうか
0. viewの書き方が間違っている
0. データがうまく伝わっていない

分かる方いたらご教示お願いします。

accepts_nested_attributes_forを使わない方がいいとかはなしでお願いします。使わないといけないので。

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

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

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

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

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

guest

回答1

0

Todo が Category と関連つけられてsaveされて居ないのが原因です。
category_params で todos_attributes: とあるのに、params にはその名前がないですね。
logを見ても CategoryのINSERTはあってもTodoのINSERTがありません。

Todoのdatabaseの値を見ても category_id がnilです。
belongs_to :category なのでそういうデータはできるはずがないのですが、できているということは、Categoryとの関連を書く前に作ったのでしょう。

f.fields_for の所がおかしいです。

投稿2021/08/20 13:08

winterboum

総合スコア23416

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

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

isd_kisk

2021/08/21 05:56

「logを見ても CategoryのINSERTはあってもTodoのINSERTがありません。」 →それをするには実際にどうするといいのでしょうか 「f.fields_for の所がおかしいです。」 →具体的に何がおかしいのでしょうか。解決策をご教示いただけると幸いです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問