嵌套表单不保存
Nested form don't save
我是 rails 和 MVC 的新手。我正在尝试将我使用 simple-form 和 nested-form-for gems 创建的嵌套表单保存到我的数据库中。
当我提交表单时,只会创建并保存 'parent' 模型。
非常感谢。
'parent model':
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
'child' 型号:
class DietCompose < ApplicationRecord
belongs_to :diet
belongs_to :food
end
'parent' 控制器:
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
'child' 控制器:
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 控制台上我收到错误
LoadError: Unable to autoload constant Diet_compose, expected
/home/tanaka/Desktop/E-xercite/app/models/diet_compose.rb to define it
from (irb):8
您需要在此处更正多项内容。我已经添加了内联评论。
在 Diet
模型中 (diet.rb
)
class Diet < ApplicationRecord
attr_accessible :diet_composes # make the attributes accessible for mass assignment.
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
# add attributes of nested association to whitelist
params.require(:diet).permit(:name, :coach_id, :diet_composes_attributes => [:hour, :day, :food_id, :diet_id])
end
end
您实际上并不需要 DietComposesController
的操作来使 Diet
的嵌套属性正常工作。但是,更正下面的错误会给你 LoadError
class DietComposesController < ApplicationController
def new
@diet_compose = DietCompose.new # Model name is always CamelCase. DietCompose rather than Diet_compose
end
def create
@diet_compose = DietCompose.new(diet_compose_params) # here as well
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
你的命令应该是DietCompose.all
。甚至看看diet_model.rb
的定义。它将名称声明为 DietModel
。
你的观点似乎不错。我没有使用 nested_form_fields
gem,所以不能 100% 确定。调试的简单规则,查看rails服务器的日志并推断:)
我是 rails 和 MVC 的新手。我正在尝试将我使用 simple-form 和 nested-form-for gems 创建的嵌套表单保存到我的数据库中。 当我提交表单时,只会创建并保存 'parent' 模型。 非常感谢。
'parent model':
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
'child' 型号:
class DietCompose < ApplicationRecord
belongs_to :diet
belongs_to :food
end
'parent' 控制器:
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
'child' 控制器:
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 控制台上我收到错误
LoadError: Unable to autoload constant Diet_compose, expected /home/tanaka/Desktop/E-xercite/app/models/diet_compose.rb to define it from (irb):8
您需要在此处更正多项内容。我已经添加了内联评论。
在 Diet
模型中 (diet.rb
)
class Diet < ApplicationRecord
attr_accessible :diet_composes # make the attributes accessible for mass assignment.
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
# add attributes of nested association to whitelist
params.require(:diet).permit(:name, :coach_id, :diet_composes_attributes => [:hour, :day, :food_id, :diet_id])
end
end
您实际上并不需要 DietComposesController
的操作来使 Diet
的嵌套属性正常工作。但是,更正下面的错误会给你 LoadError
class DietComposesController < ApplicationController
def new
@diet_compose = DietCompose.new # Model name is always CamelCase. DietCompose rather than Diet_compose
end
def create
@diet_compose = DietCompose.new(diet_compose_params) # here as well
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
你的命令应该是DietCompose.all
。甚至看看diet_model.rb
的定义。它将名称声明为 DietModel
。
你的观点似乎不错。我没有使用 nested_form_fields
gem,所以不能 100% 确定。调试的简单规则,查看rails服务器的日志并推断:)