块内的simple_form不起作用

时间:2013-05-03 19:12:43

标签: ruby-on-rails-3

我有一个生成许多表单的块,不知何故所有表单都按预期工作,除了第一个,我使用的是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:

Divs that the block generates

单击橙色按钮时,会显示以下形式:

enter image description here

当我使用第一个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方法,但不知道为什么。

2 个答案:

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

答案 1 :(得分:0)

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