how to trigger one form action with multiple tabs but different arguments for each tab (slim)

476 Views Asked by At

I have a form with multiple tabs like so:

  arg = {id: @id}    
 = form_tag({ action: "update_list" }.merge(arg), method: put) 
  .form-group
    ul.nav.nav-tabs 
      -for k,v in @list
         li.nav-item class="active"
           a.nav-link data-toggle="tab" href="##{k}" #{v}


    div class="tab-content"
      - for name in @list
          div id="#{name}" class="tab-pane fade in active"    
            textarea.form-control  name= test 

It can be seen that the form encapsulates multiple tabs and each tab has a text area and upon submitting of the form, a put request is sent with an id being used as an argument. However, each of the text areas have different ids. usually on submitting a form like this, a put request could be sent for all the tabs under one endpoint and a single argument could be used for that call. How would it work however if that argument is different for each tab? in this case, each textbox has its own unique id so multiple put requests would need to be sent to differing endpoints to submit all the tabs. How would I go about achieving this? essentially the argument being used for the form would be different for each tab.

1

There are 1 best solutions below

0
Jad On

I'm not really a slim expert, as I'm used to haml.

you'll need to define @ids in your controller. this creates a new form for each id, so that each form has a text_area in it with the name test. you might want to tweak this to put the id into the textarea name.

- @ids.each do |id|
  = form_tag({ action: "update_list", id: id }, method: put) do |form|
    .form-group
      %ul.nav.nav-tabs 
        - for k,v in @list
          %li.nav-item{class:"active"}
            %a.nav-link{'data-toggle':"tab", href:"##{k}"} #{v}

      %div{class:"tab-content"}
        - for name in @list
          .tab-pane.fade.in.active{id:"#{name}"}    
            = form.text_area :test