LoginSignup
0
0

More than 1 year has passed since last update.

ダイエット+食品管理アプリ構築の道程(2)

Last updated at Posted at 2021-06-25

前回書きました上下で送るべきViewのフォームですが
簡単にまずシンプルに作ってみて挙動を確認してからしっかりと作り込んでいくこととします。

手順①frigeモデルを作る(子モデル)

rails g model fridge(モデルは単数形)

手順②frigesコントローラーを作る

rails g controller friges(コントローラーは複数形)

手順③cookingモデルを作る(親モデル)

rails g model cooking

手順④cookingsコントローラーを作る

rails g controller cookings

20210625-130848.png

20210625-130907.png

手順⑤

(訂正前)cooking.rb
class Cooking < ApplicationRecord
  has_many  :fridge, dependent: :destroy

  accepts_nested_attributes_for :fridge
end

dependent: :destroyで親が削除されると子も削除と行きたいが、
食材は買ってきたものは別に利用できるのでこのオプションは付けない。

(訂正後)cooking.rb
class Cooking < ApplicationRecord
  has_many  :fridge

  accepts_nested_attributes_for :fridge
end

acccept_nested_attributes_forの使用は断念
理由は非推奨のため

https://moneyforward.com/engineers_blog/2018/12/15/formobject/
https://tech.recruit-mp.co.jp/server-side/rails-development-policy/

次回からFormオブジェクトでいきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0