简体   繁体   English

Rails:以表单形式向控制器提交html表内容

[英]Rails: Submit html table content in form to controller

I have some trouble with an rails formular. 我在使用Rails配方器时遇到了一些麻烦。

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. 如果我单击“ taskSubmit”,则一个javascript将该任务(标题,描述,最小值)放入项目表单内的表格中。 JS create a new table row with the task content. JS用任务内容创建一个新的表行。

Javascript Java脚本

//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. 我现在要做的是将这些表元素提交给项目控制器,以创建项目和所有必要的任务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. 直截了当,它们将很容易在params哈希中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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