Question

I have this form:

<%= form_for(@debate.debates.build) do |support_form| %>  
  <div>
    <%= support_form.label :content %><br />
    <%= support_form.text_area :content %>
  </div>
  <%= support_form.hidden_field :is_supporting, :value => is_supporting %>
  <div class="actions">
    <%= support_form.submit %>
  </div>
<% end %>

Each debate has_many debate and belongs to a debate (a tree structure) and @debate.debates.build is supposed to create a new debate that is the child of @debate, but the debate created by @debate.debates.build is always nil

When I run the same code in irb, though, the association is correctly set up, and the debate id of the new debate is its parent, the way I want it to be.

Whats going on? And how can I make sure that the new debate has its parent debates id set up correctly?

Was it helpful?

Solution

It builds it in memory, but I don't think it actually saves it until a save is run, presumably in the recipient action of the form, probably the create action of the controller.

In order for that to work, the debate id (debate_id? you don't show enough of your model) may need to be in the form as well.

Finally, the issue could be related to attr_accessible or attr_protected issues. If the debate id is not allowed to be set as part of the bulk attribute update, it may get lost in the submission.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top