0

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"}
7
  • can yo try to replace belongs_to :fooditem with belongs_to :fooditem, optional: true? Commented Dec 22, 2016 at 6:14
  • fooditem_id is mandatory Commented Dec 22, 2016 at 6:16
  • Could you print params, received by your controller and fooditem_params method here? And I don't see anything about FoodItem in your view, only FooditemPrices Commented Dec 22, 2016 at 6:32
  • ' "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"} ' Commented Dec 22, 2016 at 6:36
  • what is your rails version? Commented Dec 22, 2016 at 6:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.