【问题标题】:Ruby Nested FormRuby 嵌套表单
【发布时间】:2016-12-22 06:08:15
【问题描述】:

实际上,保存时必须保存,但我遇到错误并试图找出我犯错的错误。

请帮我解决这个错误。

提前致谢。

控制器

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

型号

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

表单数据

<%= 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 -%>

错误

fooditem must exist

收到的参数

"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"}

【问题讨论】:

  • 你可以尝试用belongs_to :fooditem, optional: true替换belongs_to :fooditem吗?
  • fooditem_id 是强制性的
  • 您能打印由您的控制器接收的参数和fooditem_params 方法吗?在你看来,我没有看到任何关于 FoodItem 的信息,只有 FooditemPrices
  • ' "fooditem"=>{"name"=>"食品名称", "bdescription"=>"简要食品描述", "ddescription"=>"详细食品描述", "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 ", "重量"=>"120", "weight_in"=>"克"}, "2"=>{"size"=>"Full", "price"=>"50", "weight"=> "60", "weight_in"=>"克"}}, "sku"=>"", "active"=>"1"}, "commit"=>"创建食物"} '
  • 你的 Rails 版本是多少?

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-5


【解决方案1】:

您使用的是 Rails 5,所以默认 belongs_to 要求关联记录必须存在。

尝试像这样在您的关联中添加inverse

class Fooditem < ApplicationRecord
  has_many  :fooditemprices, dependent: :destroy, inverse_of: :fooditem
end

class Fooditemprice < ApplicationRecord
  belongs_to :fooditem, inverse_of: :fooditemprices
end

它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2015-08-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多