Fábio Queluci
Fábio Queluci

Reputation: 311

Rails form for creating child records of self referential models

I have the following model in my Rails application:

class Todo < ActiveRecord::Base
  has_many :sub_todos, class_name: 'Todo', foreign_key: 'parent_todo_id'
  belongs_to :parent_todo, class_name: 'Todo'
end

I'm researching a way to do a WBS (work breakdown structure) app, and so far have index view:

<h1>Todos</h1>

<%= form_for @todo do |f| %>                                                                                                                                    
  <%= f.label :content, 'Todo', class: 'control-label' %>                                                                                                       
  <%= f.text_field :content, class: 'form-control', size: 50 %>                                                                                                 
  <%= f.submit 'Save' %>                                                                                                                                        
<% end %>                                                                                                                                                       

<% @todos.where(parent_todo_id: nil).each do |todo| %>                                                                                                          
  <p><%= todo.content %></p>                                                                                                                                    
  <ul>                                                                                                                                                          
    <% todo.sub_todos.each do |sub_todo| %>                                                                                                                     
      <li>                                                                                                                                                      
        <%= sub_todo.content %>
        <!--form goes here-->
      </li>

      <ul>
        <% sub_todo.sub_todos.each do |sub_todo| %>
          <li><%= sub_todo.content %></li>
        <% end %>
      </ul>
   <% end %>
  </ul>
<% end %>

I need items to be split into smaller ones. To do so, I'd like to put a form bellow each one, so that I can create a new "sub_todo".

My question is: how do I make a form for self-referential models? I've used forms for nested resources before, but I can't seem to figure out what to do in the case of self-referential models.

Also, is this the proper ruby/rails way of looping over this type of records?

Upvotes: 0

Views: 269

Answers (1)

dinomix
dinomix

Reputation: 976

You would use nested forms. There is a great RailsCast for this:

http://railscasts.com/episodes/196-nested-model-form-part-1

f.fields_for :sub_todo # in your form_for block

To add in the list with no parent form, where you have "form goes here" do

form_for :todo
  hidden_field_for :parent_todo_id, sub_todo_id

...

Upvotes: 1

Related Questions