how to submit 2 forms with one button rails 3

让人想犯罪 __ 提交于 2020-01-05 05:41:29

问题


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 ?


回答1:


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!



来源:https://stackoverflow.com/questions/12732781/how-to-submit-2-forms-with-one-button-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!