2016-02-16 53 views
1

我有Pieces模型,它是商店項目的部分。當我需要的時候,我會向用戶詢問,檢查棋子是否正常,或者是生成的表單。Form_tag不會將check_box_tag值添加到參數中

我正在創建報告模型來存儲我向用戶詢問的表單。

所以,當我生成的形式check_box和text_field工作正常。但是當我提交表單時,我無法獲得check_box值作爲參數。

{"utf8"=>"✓", 
"_method"=>"patch", 
"authenticity_token"=>"5OmdXCDixwxru2yBtB3YmT3YF/ZqOByoAJ3J29TyJikxJdA+RXAvGjwu8MBIPlJCROFx4V7AUcA7IIqIgthD9g==", 
"notes"=>{"79"=>"asdasda","84"=>""}, 
"commit"=>"Submit", 
"id"=>"5"} 

這裏是我的edit.html.erb

<%- model_class = Report -%> 
<div class="page-header"> 
    <h1><%=t '.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human.titleize %></h1> 
</div> 


    <% if @report.errors.any? %> 
    <div id="error_expl" class="panel panel-danger"> 
     <div class="panel-heading"> 
     <h3 class="panel-title"><%= pluralize(@report.errors.count, "error") %> prohibited this report from being saved:</h3> 
     </div> 
     <div class="panel-body"> 
     <ul> 
     <% @report.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
     </div> 
    </div> 
    <% end %> 
    <table class="table table-striped"> 
    <thead> 
     <tr> 
     <th><%= model_class.human_attribute_name(:status) %></th> 
     <th><%= model_class.human_attribute_name(:serial_no) %></th> 
     <th><%= model_class.human_attribute_name(:category) %></th> 
     <th><%= model_class.human_attribute_name(:name) %></th> 
     <th><%= model_class.human_attribute_name(:brand) %></th> 
     <th><%= model_class.human_attribute_name(:model) %></th> 
     <th><%= model_class.human_attribute_name(:quantity) %></th> 
     </tr> 

    </thead> 
    <tbody> 
    <%= form_tag(report_path(@report), :method => :patch) do %> 

     <% @pieces.each do |piece| %> 
     <tr> 
       <td><div class="form-group"> 
    <%= check_box_tag(:status, name: "status["+piece.id.to_s+"]", class: 'form-control') %> 
        </div></td> 
       <td><%= piece.try(:serial_no) %></td> 
       <td><%= piece.item.category.name %></td> 
       <td><%= piece.item.name %></td> 
       <td><%= piece.item.brand %></td> 
       <td><%= piece.item.model %></td> 

       <% if piece.serial_no.start_with?("D") %> 
         <td><%= piece.item.pieces.count %> <%= piece.item.unit.name %></td> 
        <% else %> 
         <td><%= piece.item.pieces.sum(:quantity) %> <%= piece.item.unit.name %></td> 
       <% end %> 
     </tr> 
     <tr> 
      <td colspan=7 class="hidden" id="hebele"> 
       <%= text_field_tag(:note,"", placeholder: "Note ", name: "notes["+piece.id.to_s+"]",class: "form-control ") %> 
      </td> 
     </tr> 
     <% end %> 
    </tbody> 
    </table> 

     <div class="form-group"> 
     <div class="col-lg-offset-2 col-lg-10"> 
      <%= submit_tag("Submit",:class => 'btn btn-primary') %> 
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")), reports_path, :class => 'btn btn-default' %> 
     </div> 
     </div> 
    <% end %> 
<script type="text/javascript"> 
    $("[id='status']").bootstrapSwitch({ 
     size: 'mini', 
     onColor: 'success', 
     offColor: 'danger', 
     indeterminate: true, 
     onSwitchChange: function(event, state) { 
     if (state == false){ 
     $("#hebele").removeClass('hidden'); 
     }else if(state == true){ 
     $("#hebele").addClass('hidden'); 
     } 
     } 
    }); 
</script> 

控制器:

def edit 
    @pieces = @report.pieces.joins(:item).group(:item_id) 
    @report = Report.find(params[:id]) 
end 

回答

0

您需要定義check_box_tag的價值:

<%= check_box_tag :status, piece.id, class: 'form-control' %> 

如果你是一個editing@report,你會使用form_for最好:

<table> 
    <tbody> 
    <%= form_for @report do |f| %> 
     <% @pieces.each do |piece| %> 
     <tr> 
      <td> 
      <div class="form-group"> 
       <%= f.check_box :status, class: 'form-control', price.id %> 
      </div> 
      </td> 
      <td><%= piece.try(:serial_no) %></td> 
      <td><%= piece.item.category.name %></td> 
      <td><%= piece.item.name %></td> 
      <td><%= piece.item.brand %></td> 
      <td><%= piece.item.model %></td> 

      <% if piece.serial_no.start_with?("D") %> 
      <td><%= piece.item.pieces.count %> <%= piece.item.unit.name %></td> 
      <% else %> 
      <td><%= piece.item.pieces.sum(:quantity) %> <%= piece.item.unit.name %></td> 
      <% end %> 
     </tr> 
     <tr> 
      <td colspan=7 class="hidden" id="hebele"> 
      <%= f.text_field :note, placeholder: "Note", class: "form-control" %> 
      </td> 
     </tr> 
    <% end %> 
    <tr><td colspan="7"><%= f.submit, class: "btn btn-primary" %></td></tr> 
    <% end %> 
    </tbody> 
</table> 
+0

謝謝@Richard,我不能使用的form_for因爲沒有:狀態還是:在它記方法。或者我不知道如何使用form_for沒有未定義的方法。 :) 另外我沒有任何價格變量。也許這是錯字?你想說一件嗎? –