Form_tag不会将check_box_tag值添加到参数中

时间:2016-02-16 23:24:31

标签: ruby-on-rails

我有Pieces模型,它是商店项目的部分。当我需要时,我会向用户询问,检查件是否正常或者生成的表单。

我正在创建报告模型来存储我向用户提出的表单。

所以,当我生成表单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

1 个答案:

答案 0 :(得分: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>