1

I have a form with a has_many association and I would like to submit multiple forms via ajax so the parent form's show page can be updated dynamically.

I have the following models (they are in mongoid but i don't think it matters between mongoid and activerecord):

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  has_many :tasks
end

class Task
  include Mongoid::Document
  include Mongoid::Timestamps
  belongs_to :user, inverse_of: :tasks
end

I have a form which renders n partials of the form:

<%= form_for @task, remote: true do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

for new tasks on the show.html.erb for user via ajax.

How can i write a single submit button on the show.html.erb to submit all of the tasks at once while associating them with the parent user?

1 Answer 1

1

Based on the second answer to this question: Rails 3: How to trigger a form submission via javascript?

Say you have a button with an id called #multiSubmit, then you can submit each form when it is clicked using the following JavaScript:

$(function() {
  $("#multiSubmit").click( function() {
    $('form').each(function(i, item) {
      $(item).trigger('submit.rails');
    });
  });
});
Sign up to request clarification or add additional context in comments.

Comments

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.