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

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

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

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

Q&A

解決済

2回答

262閲覧

RubyonRailsでaccepts_nested_attributes_forを使った登録

ai_0928

総合スコア0

Ruby on Rails

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

0グッド

0クリップ

投稿2020/05/14 13:29

編集2020/05/15 11:20

前提・実現したいこと

Ruby on Railsの勉強中です。
店舗情報を登録するシステムを作っています。
登録画面で、店舗情報と営業形態情報を入力し、それぞれのテーブルに登録したいです。

######DB設計
イメージ説明

発生している問題

登録画面では店舗名称を入力し
チェックボックスで営業形態(イートイン、テイクアウト、宅配など)を一つ以上選択します。
店舗テーブルの登録は出来るのですが、営業形態テーブルのINSERT文が発行されておらず、登録されません。
エラーは出ておりません。
ソースは以下の通りです。

該当のソースコード

######models\t_store_content.rb

ruby

1class TStoreContent < ApplicationRecord 2 has_many :t_eigyo_types, class_name: "TEigyoType", dependent: :destroy 3 has_many :m_eigyo_types, class_name: "MEigyoType", through: :t_eigyo_types 4 accepts_nested_attributes_for :t_eigyo_types 5end

######models\t_eigyo_type.rb

ruby

1class TEigyoType < ApplicationRecord 2 belongs_to :store_content, class_name: "TStoreContent", foreign_key: "t_store_content_id" , optional: true 3 belongs_to :m_eigyo_type, class_name: "MEigyoType", foreign_key: :eigyo_type_id, optional: true 4end

######models\m_eigyo_type.rb

ruby

1class MEigyoType < ApplicationRecord 2 has_many :t_eigyo_types, class_name: "TEigyoType" 3 has_many :t_store_contents, class_name: "TStoreContent", through: :t_eigyo_types 4end

######controllers\contents_controller.rb

ruby

1class ContentsController < ApplicationController 2 3 def new 4 @content = TStoreContent.new 5 TEigyoType.count.times{ @content.t_eigyo_types.build } 6 end 7 8 def create 9 @content = TStoreContent.new(permit_params) 10 11 if @content.save 12 ...省略... 13 end 14 end 15 16 private def permit_params 17 params.require(:t_store_content).permit( 18 :store_name, 19 t_eigyo_types_attributes: [:eigyo_type_id] 20 ) 21 end 22end 23

######views\contents\new.html.erb

<div id="generic-form"> <%= form_with model: @content, url: :contents, id: "contentForm" do |f| %> <%= render "form", f: f %> <div> <%= f.submit "登録する", id: "submit" %> <%= link_to "キャンセル", :root %> </div> <% end %> </div>

######views\contents_form.html.erb

<div> <%= f.label :store_name, "店舗・施設名" %> <%= f.text_field :store_name, maxlength: 50 %> </div> <div> <%= fields_for :t_eigyo_types, @content.t_eigyo_types.build do |ef| %> <%= ef.label :eigyo_type_id, "営業形態" %> <%= ef.collection_check_boxes :eigyo_type_id, MEigyoType.all.order(:sort_no), :id, :eigyo_type_name %> <% end %> </div>

試したこと

ポストパラメータには値が入っています。

{"authenticity_token"=>"...省略...", "t_store_content"=> {"store_name"=>"カフェ", ...省略...}, "t_eigyo_types"=>{"eigyo_type_id"=>["", "1", "2"]}, "commit"=>"登録する"}

様々なページを見ましたが、何が足りないのか、どこが間違っているのかは分かりませんでした。。
ヒントだけでもいただけると助かります。
よろしくお願いいたします。

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

ruby 2.6.4
rails 6.0.2.2

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

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

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

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

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

guest

回答2

0

自己解決

下記のページを参考に、eigyo_type_idの各要素に対し、ループでcreateの処理を入れると登録されました。
https://kolosek.com/carrierwave-upload-multiple-images/

投稿2020/05/18 07:38

ai_0928

総合スコア0

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

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

0

DB設計とclassの関連定義が一致していません。

class TStoreContent < ApplicationRecord has_many :t_eigyo_types, class_name: "TEigyoType", dependent: :destroy class TEigyoType < ApplicationRecord belongs_to :store_content, class_name: "TStoreContent", foreign_key: "t_store_content_id" end class MEigyoType < ApplicationRecord end

class TStoreContent < ApplicationRecord has_many :t_eigyo_types, class_name: "TEigyoType", dependent: :destroy has_many :m_eigyo_types, through: :t_eigyo_types class TEigyoType < ApplicationRecord belongs_to :store_content, class_name: "TStoreContent", foreign_key: "t_store_content_id" belongs_to :m_eigyo_type,foreign_key: :eigyo_type_id end class MEigyoType < ApplicationRecord has_many :t_eigyo_types, class_name: "TEigyoType" has_many :t_store_contents, through: :t_eigyo_types end

投稿2020/05/14 21:43

winterboum

総合スコア23401

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

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

winterboum

2020/05/14 22:35

あとちょっと気になるので、logから Paramatersを抜き出してください
ai_0928

2020/05/15 02:49 編集

ありがとうございます、試してみます。 取り急ぎParametersは以下です。 Parameters: { "authenticity_token"=>"省略", "t_store_content"=>{ "store_name"=>"カフェ" }, "t_eigyo_types"=>{ "eigyo_type_id"=>["", "1", "2"] }, "commit"=>"登録する" }
ai_0928

2020/05/15 11:25

①アドバイスに従ってモデルを修正しましたが、挙動は変わりませんでした ②http://soccer1356abc.hatenablog.com/entry/2018/09/22/210221 を参考に、t_eigyo_type.rbにoptional:trueを追加しましたが、挙動は変わりませんでした 何か見落としているのでしょうか。。
winterboum

2020/05/15 13:47

class TStoreContent  に accepts_nested_attributes_for :t_eigyo_types を追加して、 paramsが変わることを確認してください
ai_0928

2020/05/18 07:36

遅くなり申し訳ありません。 挙動が変わらず、別に簡潔なプロジェクトを作成し試しておりました。 paramsは変わらないのですが。。 下記のページを参考に、eigyo_type_idの各要素に対し、ループでcreateの処理を入れると登録されました。 https://kolosek.com/carrierwave-upload-multiple-images/ ご協力ありがとうございました。 また機会があればよろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問