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

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

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

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

Ruby on Rails 6

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

Ruby on Rails

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

Q&A

解決済

1回答

1486閲覧

【Rails】accepts_nested_attributes_forを使って子テーブルを保存したい。

Airi.

総合スコア8

Ruby

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

Ruby on Rails 6

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/10/20 05:22

編集2020/10/20 05:26

accepts_nested_attributes_forを使って子テーブル保存する方法

accepts_nested_attributes_forメソッドを使用して
createで保存するタイミングでエラーが出てしまいました。
何卒よろしくお願いいたします。

イメージ説明

params

1>> recipe_params 2=> <ActionController::Parameters 3{"title"=>"レシピ", "feature"=>"レシピ", "eat"=>"レシピ", "category_id"=>"2", "cold_date"=>"1", 4 "frozen_date"=>"1", "time"=>"1", "text"=>"レシピ", "ingredients_attributes"=> 5<ActionController::Parameters {"0"=><ActionController::Parameters {"thing_id"=>"1", 6"amount"=>"1"} permitted: true>} permitted: true>, "user_id"=>17} permitted: true> 7>>

> recipe_paramsの中にはちゃんと "thing_id"=>"1" が入っているのでデータは渡せているようです。

テーブル設計

イメージ説明
作成済みのテーブルが????マークがついているものです。

該当のソースコード

ルーティング

Ruby

1Rails.application.routes.draw do 2 devise_for :users 3 root to: 'recipes#new' 4 resources :users 5 resources :recipes do 6 resources :ingredients 7 end 8 resources :things 9end 10

モデル: recipe.rb

Ruby

1class Recipe < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to_active_hash :category 4 5 mount_uploader :image, ImageUploader 6 belongs_to :user 7 has_many :ingredients 8 accepts_nested_attributes_for :ingredients, allow_destroy: true 9 10 with_options presence: true do 11 validates :user_id 12 validates :title 13 validates :eat 14 validates :category_id, numericality: { other_than: 1, message: "を選択してください" } 15 validates :time, format: {with: /\d/} 16 #validates :recipe_id 17 validates :thing_id 18 validates :amount 19 end 20end

コントローラー:recipe_controller.rb

Ruby

1class RecipesController < ApplicationController 2 def index 3 end 4 5 def new 6 authenticate_user! 7 @recipe = Recipe.new 8 @ingredient = @recipe.ingredients.build 9 end 10 11 def create 12 @recipe = Recipe.new(recipe_params) 13 if @recipe.save #エラーが出ている箇所 14 redirect_to user_path(id: current_user) 15 else 16 render :new 17 end 18 end 19 20 private 21 def recipe_params 22 params.require(:recipe).permit( 23 :image,:title,:feature,:eat,:category_id,:cold_date,:frozen_date,:time,:text, 24 ingredients_attributes:[:id, :recipe_id, :thing_id, :amount, :_destroy]) 25 .merge(user_id: current_user.id) 26 end 27end

ビュー: recipes/new.html.erb (該当箇所のみ)

HTML.erb

1 <%#材料%> 2 <div class="row form-box"> 3 <%= f.fields_for :ingredients do |form| %> 4 <div class="forms-item col-md-3"> 5 材料 6 </div> 7 <%= form.collection_select(:thing_id, Thing.all, :id, :thing_name, { prompt: "---"}, {class:"input-area ingredients-thing-name col-md-3", id:"ingredient-thing-name"}) %> 8 <div class="input-area">単位</div> 9 <%= form.number_field :amount, class:"input-area ingredients-amount col-1",id:"ingredient-amount", min:"0" %> 10 <div class="input-area">単位</div> 11 <% end %> 12 </div> 13 <%# /材料%>

わからない事

thing_idをどこに定義する必要があるのかがわかりません。。
コントローラーやモデルのセッティングは問題ないと考えております。

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

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

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

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

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

guest

回答1

0

自己解決

理由はわかりませんが、解決できました><
おそらく、どこかにいらないスペースが入っていたのかと...

投稿2021/02/09 01:08

Airi.

総合スコア8

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問