1

I want to submit two forms with a single button.

Below if my code :-

rate.rb

belongs_to :target, :polymorphic => true
belongs_to :location

location.rb

has_many :rates

event.rb

has_many :rates, :as => :target

form.html.haml

= form_for [@event, @rate] do |form|
                    %ul
                        %li= form.radio_button :rate, "Excellent"
                        %li Excellent
                        %li= form.radio_button :rate, "Okay"
                        %li Okay
                        %li= form.radio_button :rate, "Poorly organized"
                        %li Poorly organized
                        %li= form.radio_button :rate, "Didn't happen"
                        %li Didn't happen

=form_for [@event.location, @rate] do |form|
                    %ul
                        %li= form.radio_button :rate, "Excellent"
                        %li Excellent
                        %li= form.radio_button :rate, "Okay"
                        %li Okay
                        %li= form.radio_button :rate, "Nothing special"
                        %li Nothing special

How can this be done ?

4
  • You can't, without submitting at least one form by AJAX. Commented Oct 4, 2012 at 17:45
  • What is the AJAX solution for this ? Commented Oct 4, 2012 at 17:48
  • 2
    It really is unclear why you would want to do this. Take a look at nested attributes instead Commented Oct 4, 2012 at 17:51
  • nested attributes would have worked if I had one model referring to two models, eg :- event has_many :rates, :locations, then I could have used fields_for, can you use fields_for in my case ? Commented Oct 4, 2012 at 17:54

1 Answer 1

2

I was in same situation today and did:

in form.html.haml add a link: (I wrote in ERB)

<%= link_to "Save", "#", :class => 'button_submit' %>

Assuming form ids as "form1" and "form2" in some coffee file:

rates.js.coffee

jQuery ->
  $(".button_submit").live "click", (e) ->
    e.preventDefault()
    $("#form1").trigger "submit"
    $("#form2").trigger "submit"

That's it!

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.