ruby-on-rails 为什么我会得到“undefined method `model_name' for {:model=>#< RealQuoteLine.“当名字就在那里?

xdnvmnnf  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(96)

有人能给我解释一下吗?我得到这个错误:

NoMethodError in Quotations#work undefined method `model_name' for {:model=>#<RealQuoteLine id: 303, quote_concept: "Exportación Aerea CDMX", quantity: 1, unit_price: 0.134e3, real_segment_id: 194, created_at: "2023-08-10 19:56:48.617556000 +0000", updated_at: "2023-08-10 19:56:48.617556000

报告的错误在这一行:

<%= form.fields_for(model: rql) do |line_form| %>

令我困惑的是:undefined method model_name' for {:model=>#<RealQuoteLine
这里不是有型号名称吗?我相信有一个解释和一些我没有想到的东西,这将帮助我很多。
Quotations#工作操作方法为:

<div>
  <h3>Captura de Cotización</h3>
</div>
<table class="show-table" >
  <tr>
    <td class="fw-light">Cotización:</td>
    <td><%= @quotation.id %></td>
  </tr>
  <tr>
    <td class="fw-light">Requerimiento:</td>
    <td><%= @quotation.request.description %></td>
  </tr>
  <tr>
    <td class="fw-light">Cliente:</td>
    <td><%= @quotation.request.client.name %></td>
  </tr>
</table>
<div class="container">
  <% @quotation.real_service_group.tap do |rsg| %>
    <div class="row mt-4">
      <div class="col">
        <span>Servicio:</span>
        <span><%= rsg.title %></span>
      </div>
    </div>
    <% rsg.real_segments.each do |rs| %>
    <div class="row mt-8">
      <div class="col">
        <span>Segmento:</span>
        <span><%= rs.title %></span>
      </div>
      <div class="col">
        <span>Moneda:</span>
        <span><%= rs.currency %></span>
      </div>
    </div>
    <div class="row mt-4">
      <div class="col">
        <span>Concepto</span>
      </div>
      <div class="col">
        <span>Cantidad</span>
      </div>
      <div class="col">
        <span>Precio Unitario</span>
      </div>
      <div class="col">
        <span>importe</span>
      </div>
    </div>

    <% subtotal_segmento = 0 %>

    <%= form_with model: rs, method: :put do |form| %>
      <% rs.real_quote_lines.each do |rql| %>
        <div data-controller="linea-cotizacion">
          <div class="row mt-4">
            <%= form.fields_for(model: rql) do |line_form| %>
              <div class="col">
                <%= line_form.text_field :quote_concept, value: rql.quote_concept, readonly: true %>
              </div>
              <div class="col">
                <%= line_form.text_field :quantity, class: "linea-cantidad", data: { target: 'linea-cotizacion.quantity' } %>
              </div>
              <div class="col">
                  <%= line_form.number_field :unit_price, 
                                            data: { 
                                              target: 'linea-cotizacion.unitPrice',
                                              action: 'focusout->linea-cotizacion#updateImporte'
                                            } %>
              </div>
              <div class="col">
                <%= content_tag(:span, '', id: 'importe', class: "fw-bold") %>
              </div>
              <%= line_form.hidden_field :id %>
              <%= line_form.hidden_field :real_segment_id %>
            <% end %> <%# end fields_for %>
          </div>
        </div>
      <% end %> <%# end each real_quote_lines (rql) %>
        <p style="background-color: darkgray;">
          <span class="text-end">Subtotal</span>
          <span id="subtotal-segmento"><%= number_to_currency(subtotal_segmento) %></span>
        </p>
        <div class="row mt-4">
          <div class="col-12">
            <span>
              <%= form.submit "Guardar Segmento", class: "btn btn-primary" %>
            </span>
          </div>
        </div>
    <% end %> <%# end  form_with%>
    <% end %> <%# end real_segments.each (rs) %>
  <% end %> <%# end real_service_group.tap (rsg) %>
</div>

涉及的模型和参数:

class RealSegment < ApplicationRecord
  belongs_to :real_service_group
  has_many :real_quote_lines, dependent: :destroy

  accepts_nested_attributes_for :real_quote_lines, allow_destroy: true
end
class RealQuoteLine < ApplicationRecord
  belongs_to :real_segment, class_name: "RealSegment"
end
def real_segment_params
    params.require(:real_segment).permit(:id, :title, :currency, :iva, :retention, :real_service_group_id, real_quote_lines_attributes: %i[id real_segment_id quote_concept quantity unit_price])
  end

这是我可以从错误控制台得到的:

>> rql
=> #<RealQuoteLine id: 303, quote_concept: "Exportación Aerea CDMX", quantity: 1, unit_price: 0.134e3, real_segment_id: 194, created_at: "2023-08-10 19:56:48.617556000 +0000", updated_at: "2023-08-10 19:56:48.617556000 +0000">
>> rql.class
=> RealQuoteLine (call 'RealQuoteLine.connection' to establish a connection)
>>

我试图更新real_quote_lines表行上的值,我已经尝试了几种配置,仍然无法推进.我怀疑这与rql是传递给块的参数有关:<% rs.real_quote_lines.each do |rql| %>但是什么是正确的水做它?

r8uurelv

r8uurelv1#

实际上,您在这里错误地使用了fields_for方法,这是在给自己制造麻烦。
您只需要传递要为其创建输入的关联的名称:

<%= form_with model: rs, method: :put do |form| %>
  <%= form.fields_for :real_quote_lines do |line_form| %>
    <div data-controller="linea-cotizacion">
      <div class="row mt-4">
        <div class="col">
          <%= line_form.text_field :quote_concept, readonly: true %>
        </div>
         # ...
      </div>
    </div>
  <% end %>
<% end %>

字段将在集合中遍历,并为每条记录创建输入,包括更新现有记录时id的隐藏输入。您不需要也不希望输入父ID。
该方法的用法在指南和API文档中有详细说明。

相关问题