1

My object is a Section. Each Section can have other Sections. Section responds to #index, index returns its order index. 0 if a section does not have sibling sections.

Let's assume my data structure is:

   a
 /   \
b     c

Where:

a.index = 0

b.index = 0

c.index = 1

If I navigate to /sections/a/manage-child-ordering, I want to be able to edit the order of the subsections of a. This is the code in my view:

<% if @section.children %>
    <%= form_for @section do |child| %>
      <%= f.fields_for :children, @section.children do |c| %>
        <%= c.text_field :index %>
      <% end %>
    <% end %>
<% end %>

<%= f.submit 'Save', class: 'btn-submit' %>

The error I get:

undefined method `errors' for #<Tag::ActiveRecord_Associations_CollectionProxy:0x007f23e60bdbd0>

What am I doing wrong?

2 Answers 2

1

Adding this in the model did the trick:

belongs_to :parent, class_name: 'Section'
has_many :children, class_name: 'Section', foreign_key: :parent_id

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

1 Comment

Do you mean the model?
0

form_for should not take a collection, it should take just the single model which is responsible for implementing the action of the form. For you this is probably @section ie.

= form_for @section do |f|

2 Comments

updated and updated the new error message I am getting.
Then you'll have to show more code, like where you're setting that @section var (and what to).

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.