scientiffic
scientiffic

Reputation: 9415

Conditional Rails Form Actions based on Javascript

I have a modal form using bootstrap which enables the user to add different types of embedded media. I'm embedding the forms into different tabs based on file type. I'd like the "save" button in the modal footer to submit the correct form based on which form is currently in focus. I know how to find the current tab using jquery, but how can I write a conditional for the save button based on this value?

enter image description here

For example, to submit a video, I currently have:

 <div class="modal-footer media_actions">

    <%= video_form.actions do %>
      <%= video_form.action :cancel, :label => "Close", :as => :link,
      :button_html => {:class => "btn", :href => "#", 
      :data => {:target => "#mediaEmbedModal", :toggle => "modal"}} %>

      <%= video_form.action :submit, :label => "Save", :as => :button,
      :button_html => {:class => "btn btn-primary", :id => "video_submit", :disable_with => "Saving..."}, :disable_with => "Saving..." %>
    <% end %>
    <% end %>
    <div class="videoLoadingIcon"></div>
  </div>

but if I want to be able to change the action based on the form that's currently active, how should I do this?

Upvotes: 0

Views: 90

Answers (1)

steakchaser
steakchaser

Reputation: 5249

Couple of options:

  1. Have two different submit buttons (one for each form) and then actually have this be part of the tab content instead of the modal-footer.
  2. Generate two modal footer divs and set the second one to display: none. Then through javascript toggle the visibility appropriately. I'd probably take this approach.
  3. Via javascript override the form target based on the tab that gets clicked.

Upvotes: 1

Related Questions