4

I am trying to build a form with nested resources in my rails 4 app. I am using the cocoon gem. Each step will have substeps, and I'd like to allow the user to add as many substeps to the form and he/she would like.

Step.rb

class Step < ActiveRecord::Base
  has_many :substeps
  accepts_nested_attributes_for :substeps

Substep.rb

class Substep < ActiveRecord::Base
  belongs_to :step

form code

<%= form_for :step, :url => steps_path do |f| %>
  <%= text_field(:step, :title, :value => '', class: 'fly-input input_info', placeholder: 'Process Title', id: 'step_form_title') %>
  <%= text_field(:step, :description, :value => '', class: 'fly-input input_info', placeholder: 'Process Description', id: 'step_form_description') %>
  <%= hidden_field :step, :known %>
  <%= hidden_field_tag :experiment, @experiment.id %>
  <%= f.fields_for :substep do |ff| %>
    <%= ff.text_field :description %>
  <% end %>
  <%= link_to_add_association 'Add substep', f, :substeps %>
  <%= f.submit "Done", class: "main_button" %>
<% end %>

When I do this, I get an error reading: "undefined method `reflect_on_association' for NilClass:Class" on this line

<%= link_to_add_association 'Add substep', f, :substeps %>

Any thoughts on my problem?

EDIT Changed text_field to ff.text_field based on Pavan's suggestion

Ron M
  • 773
  • 2
  • 10
  • 22
  • To which line the error is pointing to? – Pavan Jan 18 '16 at 15:23
  • The error is on the '<%= link_to_add_association 'Add substep', f, :substeps %>' line – Ron M Jan 18 '16 at 15:24
  • Can you include code for `link_to_add_association`? – Mike Szyndel Jan 18 '16 at 15:26
  • Try changing `<%= f.fields_for :substep do |ff| %>` to `<%= f.fields_for :substeps do |ff| %>` – Pavan Jan 18 '16 at 15:27
  • Your point @Pavan is correct but then also `<%= text_field :substep, :description %>` needs to be fixed to `<%= ff.text_field :substep, :description %>` – Mike Szyndel Jan 18 '16 at 15:30
  • @Pavan - I tried changing it to substeps, and still receiving the error – Ron M Jan 18 '16 at 15:31
  • @MichalSzyndel I made your modification, and I get "undefined method `merge' for :description:Symbol" both with and without @Pavan's suggestion – Ron M Jan 18 '16 at 15:31
  • Ok. now try changing `<%= link_to_add_association 'Add substep', f, :substeps %>` to `<%= link_to_add_association 'Add substep', ff, :substeps %>` – Pavan Jan 18 '16 at 15:33
  • I changed <%= ff.text_field :substep, :description %> to <%= ff.text_field :description %> . It removed the merge error, and went back to the original error – Ron M Jan 18 '16 at 15:33
  • Great, so now please show us source of `link_to_add_association` so we can fix the original error – Mike Szyndel Jan 18 '16 at 15:34
  • Shouldn't the `link_to_add_association` be inside the `fields_for` block? – taglia Jan 18 '16 at 15:34
  • @Pavan changing that gives 'undefined local variable or method `ff''. It's not defined after the end of the ff form. – Ron M Jan 18 '16 at 15:34
  • @MichalSzyndel it's defined in the cocoon gem: https://github.com/nathanvda/cocoon/blob/master/lib/cocoon/view_helpers.rb – Ron M Jan 18 '16 at 15:36

1 Answers1

5

Cocoon expects that you provide a form object as second parameter, as you do, but also expects that this second parameter will have actually Rails model instance as attribute f.object.

Your form is created just with model name so form_for :step so Cocoon raises an exception.

To solve this you should change it to form_for @step where @step can be Step.new or any other Step instance.

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
  • For some reason, my substep parameters are not being passed to my controller, but that is a separate issue I suppose. – Ron M Jan 18 '16 at 15:52
  • @Ron You need to change `<%= ff.text_field :substep, :description %>` to `<%= ff.text_field :description %>` to get it passed correctly to the controller. – Pavan Jan 18 '16 at 15:56
  • It is still only grabbing the first step listed, and not the steps created with the Add button – Ron M Jan 18 '16 at 16:07
  • @Ron I think you should ask a new question for that, this one got really messy already – Mike Szyndel Jan 18 '16 at 17:23
  • I made a second one: http://stackoverflow.com/questions/34859649/rails-cocoon-nested-form-only-receiving-one-first-nested-attribute – Ron M Jan 18 '16 at 19:05