简体   繁体   中英

Rails: Submit html table content in form to controller

I have some trouble with an rails formular.

I have a form to create "projects".

Project form

<%= bootstrap_form_for(@project) do |f| %>
...
<div class="container-fluid">   
    <%= f.static_control label:"Vordefinierte Aufgaben" do %>

    <div class="actions form-group nopadding">
      <div class="center-block">
        <%=link_to "", data: {toggle:"modal", target:".newTask-modal"} , class: "btn btn-success center-block btn-sidebar-ok", id: "singlebutton"  do%>
          <i> <span class="glyphicon glyphicon-plus pull-left btn-sidebar-icon"></span>Neue Aufgabe hinzufügen </i>
        <% end %>
      </div>
    </div>  

    <div class="task-table">
        <%=render 'tasks/table'%>
    </div>

<% end %>
</div> 
...
<!-- task Modal to create a new task -->    
   <%=render 'tasks/newTaskModal' %>
<% end %>


  <!-- Button -->
              <div class="actions form-group">
                <div class="center-block">
                  <%= button_tag( type: 'submit',  class: "btn btn-success center-block btn-sidebar-ok", id: "singlebutton")  do%>
                    <i> <span class="glyphicon glyphicon-ok pull-left"></span>Projekt anlegen </i>
                  <% end %>
                </div>
              </div>

Within this form a have a table which shows created tasks and a button to open an bootstrap modal. This modal contains a new form to create a task.

Task Form within modal

 <%= bootstrap_form_tag() do |f| %>

        <div class="col-sm-8"> 
            <%= f.text_field :task_title, label: "Titel", placeholder: "Betreff", icon: "pencil",wrapper: { class: 'icon-addon addon-md'} %>
        </div>
        <div class="col-sm-4"> 
            <%= f.number_field :task_in_min, label: "Arbeitszeit in Min", placeholder: "Bsp.: 25", icon: "time",  wrapper: { class: 'icon-addon addon-md'}%>            
        </div>

        <div class="col-sm-12"> 
            <%= f.text_area :task_description, label: "Beschreibung*", placeholder: "Aufgabenbeschreibung", icon: "pencil", rows: 10, wrapper: { class: 'icon-addon addon-md'}%>
        </div>


            <%= button_tag( type: 'button',  id:"taskSubmit", class: "btn btn-success center-block btn-sidebar-ok")  do %>
                <i > <span class="glyphicon glyphicon-ok pull-left btn-sidebar-icon"></span> Speichern  </i>
            <% end %>


 <% end %> 

If I click to "taskSubmit" a javascript put this task (title, description, min) into the table within the project form. JS create a new table row with the task content.

Javascript

//take data from task form and at a new task element to the task table in project form
$(function TaskSubmitFunction(){
$('#taskSubmit').click(function(){

    var task_title = $('#task_title').val();
    var task_description = $('#task_description').val();
    var task_in_min = $('#task_in_min').val();
    //Remove spaces from title
    var id = task_title.replace(/ /g,'');

    $('#taskTable tr:last').after('<tr id="task_'+id+'"> <td>'+task_title+'</td> <td>'+task_description+'</td><td>'+task_in_min+'</td><td> <span class="pull-right"><span data-toggle="tooltip" data-placement="right" data-original-title="Löschen"><button class="btn btn-sm btn-danger btn-colored delete-position" id="'+id+'"name="button" type="button" task="'+task_title+'"><i><span class="glyphicon glyphicon-remove"/></i></button></span></span></td></tr>');

    $('#taskTable').after('<input type="hidden" id="1" name="1" value="'+task_title+'" />');

    $('#task_title').val('');
    $('#task_description').val('');
    $('#task_in_min').val('');


    //initialize task delete
    $(function TaskDeleteFunction(){
        $('#'+id).click(function(){

            var task_title = $(this).attr('task');

            if (confirm("Soll die Aufgabe wirklich gelöscht werden?")) {
                $('#'+id).remove();
            }
            return false;
        });
    });


});
});

What I want to do now is to submit these table elements to the project controller to create the project and all necessary tasks activeRecords. Is this possible? How can I do this? Maybe create a hidden field for every table element which will be submit to the project form? But every table element consists of three values... how can I do this?

Do you have any idea?

best regards

Why not just use three hidden fields (one for each value)? Straightforward and they will be easy to access in the params hash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM