ruby-on-rails 表单提交按钮仅在重新加载后工作

nc1teljy  于 2023-10-21  发布在  Ruby
关注(0)|答案(8)|浏览(136)

我有一个索引页,建立了一个表,我试图让用户编辑行的表。我试图用最基本的方式来做到这一点--没有JavaScript、javascript等,除非Rails提供。
我的表在index方法中显示得很好,表中的最后一行有一个表单,可以用来添加新行。新的形式运作良好。每一行都有一个编辑链接,它路由到控制器的编辑方法。控制器设置要编辑的对象,并呈现索引,这次是在要编辑的行中显示一个表单。我的问题是,这个表单不会提交,但如果我刷新页面,它会提交。
页面在刷新后提交的事实非常令人困惑。我看不出刷新会有什么不同,然后点击链接(它仍然应该通过相同的路由,相同的变量对吗?),我看不出刷新前后表单html有什么区别。有人知道会发生什么吗?
我不知道什么代码,甚至开始寻找,但这里去;
index.html.erb

...
<tbody>
  <% @boms.each do |line| %>
   <% if line == @bom %>
     <%= render("form_in_table", form_objects: @bom , button_text: "Update") %>
    <% else %>
      <%= render("bom_in_table", line: line) %>
    <% end %>
  <% end %>
  <% if @bom.new_record? %>
    <%= render("form_in_table", form_objects: [@li, @bom] , button_text: "Add") %>
  <% end %>
</tbody>
...

_form_in_table.html.erb

<%= form_for(form_objects, html: {class: "form-in-table"}) do |f| %>
  <tr>
    <td><%= f.text_field :quantity %></td>
    <td colspan="2">
      <%= f.select(:part_id,
                   options_from_collection_for_select(@parts, :id, :pricebook_name),
                   prompt: "Select a Part",) %></td>
    <td></td>
    <td></td>
    <td></td>
    <td><%= f.submit(button_text, class: "btn btn-primary btn-mini") %></td>
  </tr>
<% end %>

_bom_in_table.html.erb

<tr>
  <td><%= line.quantity%></td>
  <td><%= line.part_number %></td>
  <td><%= line.part_description %></td>
  <td><%= number_to_currency(line.part_cost) %></td>
  <td><%= line.part_unit %></td>
  <td><%= number_to_currency(line.extension) %></td>
  <td><%= link_to('Edit', edit_bom_path(line)) %></td>
</tr>

boms_controller.rb

...
def edit
  @bom = Bom.find(params[:id])
  @li = @bom.line_item
  @boms = @li.boms.sorted_by_part_number
  @parts = Part.sorted_by_number
  render 'index'
end
...

如果这对破译代码/意图有用,我有line_items,parts和bom的集合; line_item有许多BOM,line_item有许多通过BOM的零件。除了部件/行项目关系外,BOM还有数量。BOM是物料清单的缩写。@li是被操作的line_item。我遇到麻烦的表单是用于查看/添加/编辑属于行项目的BOM(数量和部件)集合。
添加日志

Started GET "/line_items/8/boms" for 127.0.0.1 at 2013-10-14 14:27:27 -0400
Processing by BomsController#index as HTML
  Parameters: {"line_item_id"=>"8"}
  [1m[35mLineItem Load (0.0ms)[0m  SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1  [["id", "8"]]
  [1m[36mLineItemSubClass Load (1.0ms)[0m  [1mSELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1[0m  [["id", 8]]
  [1m[35mLineItemClass Load (4.0ms)[0m  SELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1  [["id", 1]]
  Rendered shared/_error_messages.html.erb (3.0ms)
  [1m[36mBom Load (1.0ms)[0m  [1mSELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC[0m  [["line_item_id", 8]]
  [1m[35mPart Load (0.0ms)[0m  SELECT "parts".* FROM "parts" WHERE "parts"."id" = ? ORDER BY "parts"."id" ASC LIMIT 1  [["id", 1]]
  Rendered boms/_bom_in_table.html.erb (96.0ms)
  [1m[36mPart Load (1.0ms)[0m  [1mSELECT "parts".* FROM "parts" ORDER BY "parts".number ASC[0m
  Rendered boms/_form_in_table.html.erb (103.0ms)
  [1m[35m (24.0ms)[0m  SELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8
  Rendered boms/index.html.erb within layouts/boms (477.0ms)
  Rendered layouts/_shim.html.erb (1.0ms)
  Rendered layouts/_header.html.erb (0.0ms)
  Rendered layouts/_footer.html.erb (0.0ms)
  Rendered layouts/application.html.erb (69.0ms)
Completed 200 OK in 671ms (Views: 601.0ms | ActiveRecord: 31.0ms)

Started GET "/boms/22/edit" for 127.0.0.1 at 2013-10-14 14:28:13 -0400
Processing by BomsController#edit as HTML
  Parameters: {"id"=>"22"}
  [1m[36mBom Load (0.0ms)[0m  [1mSELECT "boms".* FROM "boms" WHERE "boms"."id" = ? LIMIT 1[0m  [["id", "22"]]
  [1m[35mLineItem Load (1.0ms)[0m  SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? ORDER BY "line_items"."id" ASC LIMIT 1  [["id", 8]]
  [1m[36mLineItemSubClass Load (1.0ms)[0m  [1mSELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1[0m  [["id", 8]]
  [1m[35mLineItemClass Load (0.0ms)[0m  SELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1  [["id", 1]]
  Rendered shared/_error_messages.html.erb (0.0ms)
  [1m[36mBom Load (1.0ms)[0m  [1mSELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC[0m  [["line_item_id", 8]]
  [1m[35mPart Load (0.0ms)[0m  SELECT "parts".* FROM "parts" ORDER BY "parts".number ASC
  Rendered boms/_form_in_table.html.erb (25.0ms)
  [1m[36m (0.0ms)[0m  [1mSELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8[0m
  Rendered boms/index.html.erb within layouts/boms (41.0ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  Rendered layouts/_header.html.erb (1.0ms)
  Rendered layouts/_footer.html.erb (0.0ms)
  Rendered layouts/application.html.erb (54.0ms)
Completed 200 OK in 113ms (Views: 104.0ms | ActiveRecord: 3.0ms)

Started GET "/boms/22/edit" for 127.0.0.1 at 2013-10-14 14:28:37 -0400
Processing by BomsController#edit as HTML
  Parameters: {"id"=>"22"}
  [1m[35mBom Load (0.0ms)[0m  SELECT "boms".* FROM "boms" WHERE "boms"."id" = ? LIMIT 1  [["id", "22"]]
  [1m[36mLineItem Load (0.0ms)[0m  [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? ORDER BY "line_items"."id" ASC LIMIT 1[0m  [["id", 8]]
  [1m[35mLineItemSubClass Load (0.0ms)[0m  SELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1  [["id", 8]]
  [1m[36mLineItemClass Load (1.0ms)[0m  [1mSELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1[0m  [["id", 1]]
  Rendered shared/_error_messages.html.erb (0.0ms)
  [1m[35mBom Load (1.0ms)[0m  SELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC  [["line_item_id", 8]]
  [1m[36mPart Load (0.0ms)[0m  [1mSELECT "parts".* FROM "parts" ORDER BY "parts".number ASC[0m
  Rendered boms/_form_in_table.html.erb (5.0ms)
  [1m[35m (0.0ms)[0m  SELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8
  Rendered boms/index.html.erb within layouts/boms (27.0ms)
  Rendered layouts/_shim.html.erb (1.0ms)
  Rendered layouts/_header.html.erb (8.0ms)
  Rendered layouts/_footer.html.erb (0.0ms)
  Rendered layouts/application.html.erb (60.0ms)
Completed 200 OK in 131ms (Views: 94.0ms | ActiveRecord: 2.0ms)

Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400

Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/parts.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400

Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
llmtgqce

llmtgqce1#

我认为这是一个HTML问题,而不是Rails问题。根据此讨论,Form inside a table<form>不能放置在<table><tbody><tr>内。在移动<form>来 Package 表格并将控件放在相应的<td>中之后,表单就可以工作了。
我还是不明白为什么刷新页面会让表单工作,但是...

wsewodh2

wsewodh22#

如果是Rails 4,可能是因为Turbolinks。尝试把

data-no-turbolink="true" inside your body tag

这可能会起作用,它发生在我身上一次。

hl0ma9xz

hl0ma9xz3#

这种类型的错误最常见的是由无效的HTML生成的。错误的各种来源可能是:

  • 缺少<>
  • HTML标记未关闭
  • 孤立的HTML结束标记(没有开始标记相关);在复杂的形式中,我有额外的</div> s躺在...
  • 嵌套在tabletr标记内的表单(允许嵌套在td内)

表单帮助器需要正确嵌套,否则这些怪癖会咬你。

sc4hvdpw

sc4hvdpw4#

对于rails 5,尝试在包含表单的页面的任何链接中使用data: { turbolinks: false }
例如<%= link_to "Get in Touch", 'contact', data: { turbolinks: false } %>

pprl5pva

pprl5pva5#

尝试将data-no-turbolink=“true”放入调用表页面的链接中。

<a href="/vender" data-no-turbolink="true">

这对我来说很有用。

tjvv9vkg

tjvv9vkg6#

我想分享一下我的经验:我和你一样玩过涡轮机但突然间,我遇到了一个问题:以前的页面需要有turbolinks禁用太多,工作.在这个问题上花了很多很多时间之后,我找到了解决方案:<% f.submit %>被2 <div>与表单的其余部分分隔开!下面是一个示例:错误:

<div class="container">
    <div class="row">
        <!-- Inscription -->
        <div class="col-lg-8 contact_col">
            <div class="get_in_touch">
                <div class="section_title">Modifier une marque</div>
                <div class="contact_form_container">
                    <%= form_for @brand, url: {action: "update"} do |f| %>
                    <div class="row">
                        <div class="col-xl-12">
                            <!-- Name -->
                            <label for="contact_name">Nom de la marque</label>
                            <%= f.text_field :brand, class: "contact_input" %>
                        </div>
                        <div class="col-xl-12 last_name_col">
                            <span>
                                <%= f.label "Image de la marque" %><br />
                            </span>
                            <%= f.file_field :brand_picture %>
                        </div>
                    </div>
                </div>
            </div>
            <button class="newsletter_button trans_200">
                <%= f.submit "Modifier" %>
            </button>
            <% end %>
        </div>
    </div>
</div>

正确:

<div class="container">
    <div class="row">
        <!-- Inscription -->
        <div class="col-lg-8 contact_col">
            <div class="get_in_touch">
                <div class="section_title">Modifier une marque</div>
                <div class="contact_form_container">
                    <%= form_for @brand, url: {action: "update"} do |f| %>
                    <div class="row">
                        <div class="col-xl-12">
                            <!-- Name -->
                            <label for="contact_name">Nom de la marque</label>
                            <%= f.text_field :brand, class: "contact_input" %>
                        </div>
                        <div class="col-xl-12 last_name_col">
                            <span>
                                <%= f.label "Image de la marque" %><br />
                            </span>
                            <%= f.file_field :brand_picture %>
                        </div>
                    </div>
                    <button class="newsletter_button trans_200">
                        <%= f.submit "Modifier" %>
                    </button>
                    <% end %>
                </div>
            </div>
        </div>
    </div>
</div>
anhgbhbe

anhgbhbe7#

我想你的解决方法可能是在表单提交时重新加载。在表单中添加remote: true。这将只是为您做一个快速刷新。请注意,这不是一个解决方案,而是一个变通方案。一个推荐的解决方案将是加勒特伯内什的答案。

<%= form_for(form_objects, html: {class: "form-in-table"}, remote: true) do |f| %>
unftdfkk

unftdfkk8#

对于我来说,将form_tag保持在“table”和“tr”之外是有效的

相关问题