Ruby 嵌套表单
[英] Ruby Nested Form
本文介绍了Ruby 嵌套表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际上,在保存时必须保存,但我收到错误并试图找出我做错的错误.
请帮助我解决此错误.
提前致谢.
控制器
def new@fooditem = Fooditem.new3.times {@fooditem.fooditemprices.build}结尾#创建食物项目定义创建@fooditem = Fooditem.new(fooditem_params)如果@fooditem.saveflash[:success] = "食品创建成功."redirect_to fooditems_path别的呈现新"结尾结尾
模型
class Fooditem <申请记录has_many :fooditemprices, 依赖: :destroyaccepts_nested_attributes_for :fooditemprices, reject_if: lambda {|属性|属性['价格'].blank?},allow_destroy: true结尾class Fooditemprice <申请记录归属地:食物项目验证:大小,存在:{消息:大小必须存在."}验证:价格,存在:{消息:价格必须存在."}结尾
表单数据
<%= f.fields_for :fooditemprices do |ftp_form|%><div class="col-sm-9 row col-sm-offset-3"><div class="col-sm-3"><%= ftp_form.select :size, ["Full", "Half", "Small", "Medium", "Large"].collect { |p|[p, p] },{}, {class: "form-control"} %>
<div class="col-sm-3"><%= ftp_form.number_field :price, placeholder: "Price", class: "form-control" %>
<div class="col-sm-3"><%= ftp_form.number_field :weight, placeholder: "Weight", class: "form-control" %>
<div class="col-sm-3"><%= ftp_form.select :weight_in, ["Grams", "ml"].collect { |p|[p, p] },{}, {class: "form-control"} %>
<%结束-%>
错误
fooditem 必须存在
收到的参数
"fooditem"=>{"name"=>"食物名称","bdescription"=>"食品项目简要说明","ddescription"=>"食品项目详细描述",优先级"=>1",foodtype_id"=>1",foodcategory_id"=>1",fooditemprices_attributes"=>{"0"=>{"size"=>"Full", "price"=>"120", "weight"=>"200", "weight_in"=>"Grams"},"1"=>{"size"=>"Full", "price"=>"80", "weight"=>"120", "weight_in"=>"Grams"},"2"=>{"size"=>"Full", "price"=>"50", "weight"=>"60", "weight_in"=>"Grams"}},"sku"=>"",活动"=>1"},提交"=>创建食物项目"}
解决方案
您使用的是 Rails 5,所以默认 belongs_to
要求关联记录必须存在.
尝试在您的关联中添加 inverse
,如下所示:
class Fooditem <申请记录has_many :fooditemprices, 依赖: :destroy, inverse_of: :fooditem结尾class Fooditemprice <申请记录own_to :fooditem, inverse_of: :fooditemprices结尾
它应该可以工作.
Actually, On saving it must save, but i am getting error and tried to figure out the error where i did mistake.
Kindly help me with this error.
Thanks in Advance.
Controller
def new
@fooditem = Fooditem.new
3.times { @fooditem.fooditemprices.build}
end
#Creating Food Items
def create
@fooditem = Fooditem.new(fooditem_params)
if @fooditem.save
flash[:success] = "Food item created successfully."
redirect_to fooditems_path
else
render 'new'
end
end
Model(s)
class Fooditem < ApplicationRecord
has_many :fooditemprices, dependent: :destroy
accepts_nested_attributes_for :fooditemprices, reject_if: lambda {|attributes| attributes['price'].blank?}, allow_destroy: true
end
class Fooditemprice < ApplicationRecord
belongs_to :fooditem
validates :size, presence: { message: "Size must exists." }
validates :price, presence: { message: "Price must exists." }
end
Form data
<%= f.fields_for :fooditemprices do |ftp_form| %>
<div class="col-sm-9 row col-sm-offset-3">
<div class="col-sm-3">
<%= ftp_form.select :size, ["Full", "Half", "Small", "Medium", "Large"].collect { |p| [p, p] },{}, {class: "form-control"} %>
</div>
<div class="col-sm-3">
<%= ftp_form.number_field :price, placeholder: "Price", class: "form-control" %>
</div>
<div class="col-sm-3">
<%= ftp_form.number_field :weight, placeholder: "Weight", class: "form-control" %>
</div>
<div class="col-sm-3">
<%= ftp_form.select :weight_in, ["Grams", "ml"].collect { |p| [p, p] },{}, {class: "form-control"} %>
</div>
</div>
<% end -%>
Error
fooditem must exist
Params Received
"fooditem"=>{"name"=>"Food Item Name",
"bdescription"=>"Food Item description of brief",
"ddescription"=>"Food Item description of detailed",
"priority"=>"1",
"foodtype_id"=>"1",
"foodcategory_id"=>"1",
"fooditemprices_attributes"=>{
"0"=>{"size"=>"Full", "price"=>"120", "weight"=>"200", "weight_in"=>"Grams"},
"1"=>{"size"=>"Full", "price"=>"80", "weight"=>"120", "weight_in"=>"Grams"},
"2"=>{"size"=>"Full", "price"=>"50", "weight"=>"60", "weight_in"=>"Grams"}},
"sku"=>"",
"active"=>"1"},
"commit"=>"Create Food Item"}
解决方案
You are using Rails 5, so default belongs_to
requires association record must be exist.
Try to add inverse
in your association like this:
class Fooditem < ApplicationRecord
has_many :fooditemprices, dependent: :destroy, inverse_of: :fooditem
end
class Fooditemprice < ApplicationRecord
belongs_to :fooditem, inverse_of: :fooditemprices
end
and it should work.
这篇关于Ruby 嵌套表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!