Я новичок в рельсах и MVC. Я пытаюсь сохранить в своей базе данных вложенную форму, которую я создал, используя простые формы и вложенные формы для драгоценных камней. Когда я отправляю форму, создается и сохраняется только «родительская» модель. Большое спасибо.
«родительская модель»:
class Diet < ApplicationRecord
belongs_to :coach
has_many :user_eat_diets
has_many :user, through: :user_eat_diets
accepts_nested_attributes_for :user_eat_diets
has_many :diet_composes
has_many :foods, through: :diet_composes
accepts_nested_attributes_for :diet_composes
end
«детская» модель:
class DietCompose < ApplicationRecord
belongs_to :diet
belongs_to :food
end
«родительский» контроллер:
class DietsController < ApplicationController
def new
@diet = Diet.new
@diet.diet_composes.build
end
def create
@diet = Diet.new(diet_params)
if @diet.save
flash[:success] = "success"
end
end
def diet_params
params.require(:diet).permit(:name, :coach_id, :diet_composes_attributes)
end
end
«дочерний» контроллер:
class DietComposesController < ApplicationController
def new
@diet_compose = Diet_compose.new
end
def create
@diet_compose = Diet_compose.new(diet_compose_params)
if @diet_compose.save
flash[:success] = "success"
end
end
def diet_compose_params
params.require(:diet_compose).permit(:quantity, :hour, :day, :food_id, :diet_id)
end
end
вид формы:
<%= simple_form_for @diet, :html => {:class => 'form-basic' } do |f| %>
<%= f.input :name %>
<%= f.input :coach_id %>
<%= f.nested_fields_for :diet_composes do |ff| %>
<%= ff.remove_nested_fields_link %>
<%= ff.input :hour %>
<%= ff.input :day %>
<%= ff.input :food_id %>
<%= ff.input :diet_id %>
<% end %>
<%= f.add_nested_fields_link :diet_composes %>
<%= f.button :submit %>
<% end %>
Кроме того, когда я делаю команду
Diet_compose.all
на консоли rails я получил ошибку
Ошибка загрузки: невозможно автоматически загрузить константу Diet_compose, ожидается /home/tanaka/Desktop/E-xercite/app/models/diet_compose.rb, чтобы определить ее из (irb):8