0

我有一个生成许多表单的块,并且所有表单都按预期工作,除了第一个,我使用的是 Rails 3、simple_form 和 twitter-bootstrap,我尝试删除 twitter-bootstrap 部分,但它具有相同的行为.

这是代码:

<div class="well span4"><h4>Billetes</h4>
<% @paper_bills.each do |pb| %>
    <li class="well well-small">
        <div><%= pb.name %>

        <!-- We show the quantity of paper_bills -->
            <% if @corte.paper_bills != nil %>
                <% @corte.corte_paper_bills.each do |cp| %>
                    <% if cp.paper_bill_id == pb.id %>
                        <p>ID: <%= cp.id %></p>
                        <h6>Cantidad: <%= cp.quantity %></h6>
                        <h5>Suma: <%= cp.suma %></h5>
                        <!-- Modal -->
                        <div id=<%= "modal_#{pb.id}" %> class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                          <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">Billetes de <%= pb.name %></h3>
                          </div>
                          <div class="modal-body">
                            <p>
                                <%= simple_form_for(@corte_paper_bill, url: corte_paper_bill_path) do |ff| %>
                                    <%= ff.error_notification %>
                                    <%= ff.input :cp_id, 
                                                    input_html: {value: cp.id}, 
                                                    as: :hidden %>
                                    <%= ff.association :corte, 
                                                    input_html: {value: @corte.id},
                                                    as: :hidden %>
                                    <%= ff.association :paper_bill, 
                                                    input_html: {value: pb.id},
                                                    as: :hidden %>
                                    <%= ff.input :quantity %>
                            </p>
                          </div>
                          <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
                                    <%= ff.button :submit %>
                            <% end %>
                          </div>
                        </div>

                        <!-- Button to trigger modal -->
                        <div style="float: right; margin-top: -80px;">
                            <a href=<%= "#modal_#{pb.id}" %> role="button" class="btn btn-warning" data-toggle="modal">Actualizar</a>
                        </div>

                    <% end %>
                <% end %>
            <% end %>
        </div>



        <br />


    </li>
<% end %>

以下是适用于表单的操作:

def corte_paper_bill
  @corte_paper_bill = CortePaperBill.find(params[:corte_paper_bill][:cp_id])
  attr = params[:corte_paper_bill].except(:cp_id)
  @corte_paper_bill.update_attributes(attr)
  value = @corte_paper_bill.paper_bill.value
  @corte_paper_bill.suma = @corte_paper_bill.quantity * value
  @corte_paper_bill.save

  respond_to do |format|
    format.html { redirect_to edit_corte_path(@corte_paper_bill.corte_id) }
    format.json { head :no_content }
  end
end

以下是该块生成的 2 个 div:

块生成的 div

单击橙色按钮时,它会显示以下表单:

在此处输入图像描述

当我使用第一个 div 生成的表单时,它不起作用,它将我重定向到不同的页面,而不是操作应该将我重定向到的页面。

当我使用块生成的任何其他 div 的形式时,它按预期工作。

有人可以帮我找出为什么它不起作用吗?

先感谢您

编辑:我发现当我使用无法正常工作的表单时,rails 服务器终端会显示:

    Started PUT "/cortes/8" for 127.0.0.1 at 2013-05-04 10:40:46 -0500
Processing by CortesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"e2k7tN+L0tzQPUyOXJTmTjEnx+2jXzisR/KCa0VSd7A=", "corte_paper_bill"=>{"cp_id"=>"30", "corte_id"=>"8", "paper_bill_id"=>"1", "quantity"=>"10"}, "commit"=>"Create Corte paper bill", "id"=>"8"}
  Corte Load (0.4ms)  SELECT "cortes".* FROM "cortes" WHERE "cortes"."id" = $1 LIMIT 1  [["id", "8"]]
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
Redirected to http://localhost:5555/cortes/8
Completed 302 Found in 4ms (ActiveRecord: 0.7ms)

当我使用其他形式时,它会显示:

Started POST "/corte_paper_bill" for 127.0.0.1 at 2013-05-04 10:49:52 -0500
Processing by CortesController#corte_paper_bill as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"e2k7tN+L0tzQPUyOXJTmTjEnx+2jXzisR/KCa0VSd7A=", "corte_paper_bill"=>{"cp_id"=>"31", "corte_id"=>"8", "paper_bill_id"=>"2", "quantity"=>"10"}, "commit"=>"Create Corte paper bill"}
  CortePaperBill Load (0.7ms)  SELECT "corte_paper_bills".* FROM "corte_paper_bills" WHERE "corte_paper_bills"."id" = $1 LIMIT 1  [["id", "31"]]
   (0.1ms)  BEGIN
   (0.3ms)  UPDATE "corte_paper_bills" SET "quantity" = 10, "updated_at" = '2013-05-04 15:49:52.663944' WHERE "corte_paper_bills"."id" = 31
   (16.7ms)  COMMIT
  PaperBill Load (0.3ms)  SELECT "paper_bills".* FROM "paper_bills" WHERE "paper_bills"."id" = 2 LIMIT 1
   (0.1ms)  BEGIN
   (0.2ms)  UPDATE "corte_paper_bills" SET "suma" = 5000.0, "updated_at" = '2013-05-04 15:49:52.692300' WHERE "corte_paper_bills"."id" = 31
   (9.6ms)  COMMIT
Redirected to http://localhost:5555/cortes/8/edit
Completed 302 Found in 44ms (ActiveRecord: 27.9ms)

“提交”是不同的,一个使用 PUT 另一个使用 POST 方法,但不知道为什么。

4

2 回答 2

0

由于代码太长,我没有阅读详细信息。

但我认为问题在于您的each方法和simple_form_for标签:

 <% @corte.corte_paper_bills.each do |cp| %>

<%= simple_form_for(@corte_paper_bill, url: corte_paper_bill_path) do |ff| %> 

这些代码一起会带来问题,因为表单代码会生成一个具有给定特定 id的表单。这就是为什么只有一种形式可以工作的原因。

解决方案是在简单的表单标签中分配不同的 id

 <%= simple_form_for(@corte_paper_bill, url: corte_paper_bill_path, 
   html: {id: "a_custom_id_change_in_each") do |ff| %> 
于 2013-05-03T19:38:49.210 回答
0

问题是我嵌套了 2 个表单,因为我不需要嵌套它们,我将它们分开并且表单工作正常。

于 2013-05-04T17:16:10.957 回答