2015-07-21 20 views
0

我對Rails公式有一些問題。Rails:向控制器提交HTML表格內容

我有一個窗體來創建「項目」。

項目形式

<%= 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> 

在這個形式的有一個表,表示創建的任務和一個按鈕打開一個自舉模式。這個模式包含一個新的表單來創建一個任務。

任務中的模式

<%= 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 %> 

表如果我點擊「taskSubmit」一個javascript把這個任務(標題,描述,分鐘)到表中的項目表單中。 JS用任務內容創建一個新的表格行。

的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; 
     }); 
    }); 


}); 
}); 

我想現在要做的就是將這些表元素提交項目控制器創建項目和所有必要的任務activeRecords。這可能嗎? 我該怎麼做?也許爲每個要提交給項目表單的表元素創建一個隱藏字段?但每個表格元素都由三個值組成......我該如何做到這一點?

你有什麼想法嗎?

問候

+0

您似乎在正確的軌道上,雖然我似乎無法找到主窗體的提交按鈕。假設它在那裏,當你提交表單時,這些任務是否在'params'中顯示? –

+0

我已經添加了提交按鈕代碼。但是,如何爲任務創建隱藏字段(由3個值組成),以便將其提交給項目控制器? – Kumaro

+0

只需使用Rails hidden_​​field_tag幫助程序(文檔[這裏](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_​​field_tag))。 –

回答

0

爲什麼不直接使用三個隱藏字段(每個價值)?直截了當,他們將很容易訪問params散列。