○解決したい事
mysqlのmaterialsテーブル、recipi_materialsテーブルにデータを保存したいです。
現在、recipisテーブルにはデータ保存がされておりますが、上記2つのテーブルには全くデータが反映されていない状態です。
recipis controller.rb
class RecipisController < ApplicationController def index @recipi = Recipi.all.order("DESC") end def new @recipi = Recipi.new @recipi.materials.build end def create @recipi = Recipi.new(recipi_params) @recipi.save redirect_to"/" end private def recipi_params params.require(:recipi).permit(:image, :title, :content, :content_image, materials_attributes: [:id, :materials, :quantity]).merge(user_id: current_user.id) end end
new.html.haml
= form_with model: @recipi, local:true do |f| .post %h1 レシピを投稿して下さい! .post_recipi .post_recipi-left .post_recipi-left_title .post_recipi-left_title-subject レシピのタイトル =f.text_field :title, placeholder:"タイトルを入力して下さい", class:"recipi__title" .post_recipi-image =f.label :content_image, class:"label_image" =f.file_field :content_image, class:"image_field" =f.hidden_field :image_cache .post_recipi-content =f.text_area :content .post_recipi-contents .recipi_items-main .recipi_items-main_subject 材料名を入力して下さい(豚バラ肉など) =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" =f.text_field :materials, class:"recipi_items-materials",value:"" .recipi_items-quantity .recipi_items-quantity_subject 分量を入力して下さい(大さじ1、100gなど) =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" =f.text_field :quantity, class:"recipi_items-quantity_content" .submit =f.submit :"送信"
materials.rb(モデル)
class Material < ApplicationRecord has_many :recipi_materials has_many :recipis, through: :recipi_materials end
recipi.rb(モデル)
class Recipi < ApplicationRecord belongs_to :user has_many :recipi_materials has_many :materials, through: :recipi_materials accepts_nested_attributes_for :materials mount_uploader :image, ImageUploader end
recipi_material.rb(モデル)
class RecipiMaterial < ApplicationRecord belongs_to :recipi belongs_to :material end
追記
その後、recipiモデル、materialモデルに、optional:trueを付属させてみましたが、状況に変化なしです。
初心者で、おかしな文法を使用しているかもしれませんが、何卒ご教授の程宜しくお願い致します。
あなたの回答
tips
プレビュー