【问题标题】:Submitting multiple forms in one click一键提交多个表单
【发布时间】:2021-05-13 17:58:57
【问题描述】:

我在看下面的问题,它对我要问的问题有一个很好的答案,但我有一个疑问。我还不能发表评论,所以我再发一个帖子。

How to submit multiple, duplicate forms from same page in Rails - preferably with one button

这是我对表单的看法

new.html.erb

<h1 class="page-title">Nuevo Precio</h1>
<hr class="title-division">

<div class="alta-form-container">
<%= form_tag "/precios" do %>
    <% 2.times do %>
        <%= render 'form', precio: @precio %>
    <% end %>
    <%= submit_tag "Submit", class: "btn btn-success" %>
<% end %>

<%= link_to 'Lista Precios Cliente', client_precios_path %> <br><%= link_to 'Ver', [@client, @precio] %> <br>
</div>

还有我的_form.html.erb

<div class="field">
  <%= label_tag :precio, "Precio" %>
  <%= text_field_tag "precios[][precio]" %>
</div>

<div class="field">
  <%= text_field_tag "precios[][cant_acomodato]" %>
</div>

<div class="field">
  <%= hidden_field_tag "precios[][client_id]" %>
</div>

当我提交表单时,我收到一个错误提示

'没有路线匹配 [POST] "/precios"'

我猜是因为在我的 new.html.erb 上我写了 form_tag "/precios" do

对我应该更改或编辑的内容有什么想法吗?提前致谢

【问题讨论】:

    标签: html ruby-on-rails


    【解决方案1】:

    实际上你这里只有一个表单,因为 _form.html.erb 只会生成输入,根据下面的表单

    <%= form_tag "/precios" do %>
    

    会生成

    form action="/precios" method="post">
    

    如果你想保持这样,你应该添加到你的 config/routes.rb

    post '/precios', to: 'controller_name#method_name', as: :controller_name_method_name
    

    你的情况:

        post '/precios', to: 'PreciosController#create', as: :precios_create
    

    也检查一下 documentation

    【讨论】:

    • 所以我将该路由添加到 routes.rb 并收到此错误:“PreciosController”不是受支持的控制器名称。这可能导致潜在的路由问题。见guides.rubyonrails.org/…
    • 因为 RAILS 认为这个名字违反了控制器的约定(PrecioS - 像复数,应该是单数)命名检查这个guides.rubyonrails.org/…@FknCharlie
    猜你喜欢
    • 2014-02-06
    • 2014-06-15
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2019-07-25
    • 2020-11-03
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多