nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 385 forks source link

Child form is not saving #538

Closed sonq closed 5 years ago

sonq commented 5 years ago

Hello there!

I know there are millions of issues like this and i know the issue is related to either a typo mistake or something about divs but i just cannot find it or i made another mistake and i cannot find it. So, please help me out.

Here is my model for parent form

class Diet < ApplicationRecord
  validates :die_desc, presence: true
  validates :die_type, presence: true
  has_many :dietlines, inverse_of: :diet
  accepts_nested_attributes_for :dietlines, reject_if: :all_blank, allow_destroy: true
  belongs_to :type
  belongs_to :user 
end

and this is model of the nested form

class Dietline < ActiveRecord::Base
  belongs_to :diet
  belongs_to :product

end

and here are my strong params

   def diet_params
        params.require(:diet).permit( :die_desc,
                                       :die_text,
                                       :type_id,
                                       :user_id,
                                       :die_type,
                                       dietlines_attributes: [:id,
                                                              :dil_desc,
                                                              :dil_meal,
                                                              :diet_id,
                                                              :product_id,
                                                              :dil_calories,
                                                              :dil_protein,
                                                              :dil_totalcarbs,
                                                              :dil_fat,
                                                              :dil_vita,
                                                              :dil_vitc,
                                                              :dil_calcium,
                                                              :dil_iron,
                                                              :dil_sodium,
                                                              :dil_potassium,
                                                              :dil_chol,
                                                              :dil_sugars,
                                                              :dil_fiber,
                                                              :_destroy])
    end

this is the _form view

<h2>Meals</h2>

         <br/>
         <%= f.fields_for :dietlines do |dietline| %>
           <%= render 'dietline_fields', f: dietline %>
         <% end %>

         <div>
            <br/>
            <%= link_to_add_association 'Add Line', f, :dietlines,class: "btn btn-outline-success" %>
            <%= f.submit  class: 'btn btn-outline-success' %>
         </div>
         <br/>

and finally this is the cocoon fields partial

<div class = "nested-fields">

 <div class ='container'>

  <div class="row">
      <div class="col col-lg-6">
         <%= f.autocomplete_field :dil_desc, autocomplete_product_pro_desc_dietlines_path, class: 'largeinput' %>
      </div>

        <div class="col col-lg-4">
           <%= f.select :dil_meal, ['Breakfast','Lunch', 'Dinner','Snack'], {prompt: "Select Meal"},{class: "specialdeviseinput"}%>
        </div>

        <div class="col col-lg-2">
          <div class ='linedelete'>
           <%= link_to_remove_association 'Delete', f, class: 'btn btn-outline-danger'  %>
           </div>
        </div>

      <br/>

  </div>

 </div>
</div>

any help will be appreciated!

nathanvda commented 5 years ago

Could you show us the part of the logfile, I would like to see what is posted to the controller and the possible error, if any?

sonq commented 5 years ago

nathanvda please find log, it shows no error or rollback but it does not attempt to save dietlines as i see.


Started POST "/diets" for 127.0.0.1 at 2019-02-25 11:21:44 +0300
Processing by DietsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"VbLYy1T7YL3ZNxEvKgKWq6En6luYivvv8ouyqQmJePlESVePrTWyJ/f7KYGUUDCLxwEPknFaxNhS/gn3RYNu+A==", "diet"=>{"die_desc"=>"test", "die_type"=>"Keto", "die_text"=>""}, "commit"=>"Create Diet"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 8], ["LIMIT", 1]]
  ↳ /Users/sonerkurceren/.rvm/rubies/ruby-2.4.2/lib/ruby/gems/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/quiet_assets.rb:13
   (0.2ms)  BEGIN
  ↳ app/controllers/diets_controller.rb:30
  Type Load (0.3ms)  SELECT  "types".* FROM "types" WHERE "types"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/diets_controller.rb:30
  Diet Create (0.3ms)  INSERT INTO "diets" ("die_desc", "die_text", "type_id", "user_id", "die_type") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["die_desc", "test"], ["die_text", ""], ["type_id", 1], ["user_id", 8], ["die_type", "Keto"]]
  ↳ app/controllers/diets_controller.rb:30
   (2.0ms)  COMMIT
  ↳ app/controllers/diets_controller.rb:30
Redirected to http://localhost:3000/diets/36/edit
Completed 302 Found in 8ms (ActiveRecord: 3.2ms)
nathanvda commented 5 years ago

Did you notice in the post-params it does not contain any child-data? This seems to suggest your html is incorrect, possibilities:

I am guessing in this case you will have to look at the first?

sonq commented 5 years ago

Dear Nathanvda,

thanks for your helps, i deleted all the divs and wrote them one by one. Now it works.

Thanks.

nathanvda commented 5 years ago

Great work! :clap: :clap: