3

Basiclly i have the same issue like @user1224344 here:

How to submit multiple, duplicate forms from same page in Rails - preferably with one button

And the first answer looks quite nice, but i as rails beginner have problems to transpose it in my project. Ok i have two forms with the same controller that should be saved with only one submit button:

<table width="100%">
<tr>
<%= form_for([@patient, @patient.treatments.build]) do |f| %>
<th><%= f.collection_select :category_id, Category.find(:all), :id, :typ %></th>
<th><%= f.text_field :content %></th>
<th><%= f.hidden_field :note, :id => "canvascontent" %></th>
<th><%= f.text_field :day, :value => Date.today %></th>
<th><%= f.submit  :class => 'btn btn-small btn-primary', :onclick => "sketch.toDataURL()"  %></th>
<th><input type="button" onclick="sketch.clearRecording()" class="btn btn-small btn-danger" value="Löschen"></th>
<% end %>
</tr>
</table>


<table width="100%">
<tr>
<%= form_for([@patient, @patient.treatments.build]) do |f| %>
<th><%= f.collection_select :category_id, Category.find(:all), :id, :typ %></th>
<th><%= f.text_field :content , :id => "inputbox"%></th>
<th><%= f.text_field :day, :value => Date.today %></th>
<th><%= f.submit  :class => 'btn btn-small btn-primary'%></th>
<% end %>
</tr>
</table>

Thanks for help! Espacially on sunday night(at least here in germany)

Community
  • 1
  • 1
Em Sta
  • 1,616
  • 8
  • 28
  • 41

1 Answers1

4

You're fairly close.

The trick is that both forms should be nested within the form that submits them, which should be another model. I don't know how your app is put together, but I'll assume that Patients have many Treatments. Your models should include this:

patient.rb

attr_accessible :treatments_attributes, etc...
has_many :treatments
accepts_nested_attributes_for :treatments

treatment.rb

belongs_to :patient

As you can see, patient accepts the attributes for its treatments (thus the third and first line in that model). So you actually need to wrap the treatment forms in a patient form, so that you're submitting the patient form with its nested treatments. Something like this:

<%= form_for @patient do |f| %>
  <%= f.fields_for @patient.build_treatment do |tf| %>
    <%= render 'treatment_form', locals: { form: tf } %>
  <% end %>
  <%= f.fields_for @patient.build_treatment do |tf| %>
    <%= render 'treatment_form', locals: { form: tf } %>
  <% end %>
  <%= f.submit %>
<% end %>

So you've got that one form for the patient that's submitting both treatment forms, automatically associated with the patient. I may have messed up some of the specifics there, but that's the basic idea.

EDIT -- you may want to check this out. It's best to put the building of the treatment form objects in your controller like they do in that question. And you may want to check out the Rails API for more specific help on accepts_nested_attributes_for.

Also, in case it's unclear, the "locals" thing is just passing the treatment form object to a partial under the variable name "form", so in that partial, you'd write <%= form.label :whatever %>... etc, within that partial.

If you build out the form objects in your controller --

@patient.build_treatments #may not be exactly this, but it's close

-- then you can do this in your view:

<%= f.fields_for :treatment do |tf| %>

Also, if it's not clear, your partial would look something like this, based on your above code:

<table width="100%">
  <tr>
    <th><%= form.collection_select :category_id, Category.find(:all), :id, :typ %></th>
    <th><%= form.text_field :content , :id => "inputbox"%></th>
    <th><%= form.text_field :day, :value => Date.today %></th>
  </tr>
</table>

Other layout that might be more intuitive:

main view

<%= form_for @patient do |f| %>
  <%= render 'treatment_form', form: f %>
<% end %>

partial view

<%= form.fields_for :treatment do |field| %>
  <% field.label :whatever %> #...

In other words, you'd move the call to fields_for inside the partial, which might make more sense. Shouldn't actually change how anything really works.

Community
  • 1
  • 1
Sasha
  • 5,308
  • 7
  • 47
  • 87
  • Man that looks so good! You really helped me! And incidentally you have an very cool profile pic! But i have an little problem, in the partial _treatment_form i get the error: undefined local variable or method `form' for – Em Sta Jul 21 '13 at 19:11
  • 1
    Huh. That should work. That's what we're doing with "locals". Basically "locals" is a hash of variables local to the partial. You're saying "form" will be a variable equal to "tf" in that partial. At any rate, you should also be able to do Let me know if that works. – Sasha Jul 21 '13 at 19:16
  • Ok now it worked with form: tf! I will test it and then i rate your answer! Thanks again! Are you professional Rails developer? – Em Sta Jul 21 '13 at 19:19
  • 1
    Ha. Recently started, yeah. Glad that helped. I learned rails/coding not too long ago, and I remember it all being super confusing, so hopefully that cleared this up a bit. – Sasha Jul 21 '13 at 19:20
  • Ok your answer was in any case correct, and very good! But so far it wont really work, because something in the patient controller is not defined but i moved that to another question http://stackoverflow.com/questions/17776091/unknownattributeerror-in-controller – Em Sta Jul 21 '13 at 19:35