提问人:Fernando Flores 提问时间:9/29/2023 最后编辑:Fernando Flores 更新时间:9/30/2023 访问量:56
我怎么会得到{:model=>#<RealQuoteLine 的“未定义方法'model_name'......“ 当名字就在那里时?
How comes I get "undefined method `model_name' for {:model=>#<RealQuoteLine ... " when the name is right there?
问:
有人可以向我解释一下吗?我收到此错误:
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#work 操作方法是:
<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| %>
答:
2赞
max
9/29/2023
#1
实际上,您只是通过错误地使用该方法而使自己感到困难。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 的输入。
评论
0赞
max
9/29/2023
顺便说一句,请记住可访问性 - 提供标签并使用实际的交互元素(如按钮),而不是将行为拍打到跨度上。
0赞
max
9/30/2023
@engineersmnky我刚刚删除了手动分配。我不确定问题是否如此之大,以至于它是一个隐式哈希,因为它没有与 相同的签名。它需要输入的名称和可选集合,或者需要支持 ActiveModel::Naming API 的模型。fields_for
form_with
0赞
Fernando Flores
10/4/2023
刚刚注意到一些奇怪的事情:当使用 <%= form.fields_for 时:real_quote_lines do |line_form|%> 重复 THE FIRST 段的quote_lines???为什么?如果我使用 <%= form.fields_for rql do |line_form|%> 行不会重复,但数量不会更新...
0赞
Fernando Flores
10/4/2023
最后,我使用以下方法得到了所需的结果: <%= form.fields_for :real_quote_lines, rql do |line_form|%> 正如文档所说......
0赞
max
10/4/2023
这应该没有必要。你正在做一些古怪的事情——但我目前真的看不出你在编码什么
评论
fields_for
正在预测模型,但您传递的是 .尝试将其更改为或查看此处链接的文档,以获取有关如何实现此功能的更多详细信息form.fields_for(model: rql) do |line_form|
Hash
form.fields_for(rql) do |line_form|