Merge two forms rails -
basiclly have same issue @user1224344 here:
how submit multiple, duplicate forms same page in rails - preferably 1 button
and first answer looks quite nice, rails beginner have problems transpose in project. ok have 2 forms same controller should saved 1 submit button:
<table width="100%"> <tr> <%= form_for([@patient, @patient.treatments.build]) |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]) |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 help! espacially on sunday night(at least here in germany)
you're close.
the trick both forms should nested within form submits them, should model. don't know how app put together, i'll assume patients have many treatments. 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 can see, patient accepts attributes treatments (thus third , first line in model). need wrap treatment forms in patient form, you're submitting patient form its nested treatments. this:
<%= form_for @patient |f| %> <%= f.fields_for @patient.build_treatment |tf| %> <%= render 'treatment_form', locals: { form: tf } %> <% end %> <%= f.fields_for @patient.build_treatment |tf| %> <%= render 'treatment_form', locals: { form: tf } %> <% end %> <%= f.submit %> <% end %>
so you've got 1 form patient that's submitting both treatment forms, automatically associated patient. may have messed of specifics there, that's basic idea.
edit -- may want check this out. it's best put building of treatment form objects in controller in question. , may want check out rails api more specific on accepts_nested_attributes_for.
also, in case it's unclear, "locals" thing passing treatment form object partial under variable name "form", in partial, you'd write <%= form.label :whatever %>... etc, within partial.
if build out form objects in controller --
@patient.build_treatments #may not this, it's close
-- can in view:
<%= f.fields_for :treatment |tf| %>
also, if it's not clear, partial this, based on 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 might more intuitive:
main view
<%= form_for @patient |f| %> <%= render 'treatment_form', form: f %> <% end %>
partial view
<%= form.fields_for :treatment |field| %> <% field.label :whatever %> #...
in other words, you'd move call fields_for inside partial, might make more sense. shouldn't change how works.
Comments
Post a Comment