【问题标题】:Template::Error undefined method `[]' for nil:NilClassTemplate::Error undefined method `[]' for nil:NilClass
【发布时间】:2020-06-24 00:56:44
【问题描述】:

您好,我有一个生产中的 Rails 应用程序,最近我开始在加载我的“投诉”控制器的 show 方法时遇到完全随机的错误。

我的“投诉”表与“交互”表是一对一的关系,您可以创建交互然后创建投诉。

我在投诉#index 的表格中显示我的数据,然后单击一行以访问投诉#show

我收到此错误:

ActionView::Template::Error (undefined method `[]' for nil:NilClass):
     7:       <div class="uk-width-expand@m">
     8:         <h1 class="uk-heading-primary">
     9:           <%# <%= link_to 'Retour à l\'interaction', interaction_path(@variables[:complaint].interaction, search: @params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
    10:           <%= I18n.t("kind.#{@variables[:complaint].kind}") %>
    11:           <% if @variables[:complaint].interaction.date.present? %>
    12:             - <%= @variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
    13:           <% end %>
  
app/views/complaints/show.html.erb:10:in `_app_views_complaints_show_html_erb__763547916_154685640'

但此错误完全随机出现。这意味着如果我刷新页面,这一次它可能会启动。所以正因为如此,我不明白这个错误是从哪里来的,大多数时候没有错误,但我收到应用用户的抱怨,说他们真的很烦。

这是我的控制器显示方法:

def show
complaint = Complaint.find_by(id: params[:id])
if complaint.blank?
  redirect_to complaints_path
  return
end
interaction = Interaction.find(complaint.interaction.id)
result = DetailCommande.execute_procedure "p_detail_commande", interaction.do_piece, interaction.do_type
contenu_commande = DetailCommande.execute_procedure "p_contenu_commande", interaction.do_piece, interaction.do_type
@variables = { complaint: complaint, detail_commande: result[0], contenu_commande: contenu_commande }
@params = { search: params[:search] }
end

这是我的抱怨#show 视图中引起麻烦的部分:

    <div class="uk-flex-middle uk-margin-small" uk-grid>
  <div class="uk-width-expand@m">
    <h1 class="uk-heading-primary">
        <%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: @params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
        <%= I18n.t("kind.#{@variables[:complaint].kind}") %>
      <% if @variables[:complaint].interaction.date.present? %>
        - <%= @variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
      <% end %>
    </h1>
  </div>
  <div class="uk-width-auto@m">
    <% if @variables[:complaint].control.present? %>
      <button class="uk-button uk-button-default" disabled>Contrôle</button>
    <% else %>
      <a href="#modal-control-<%= params[:id] %>" class="uk-button uk-button-warning" uk-toggle>Contrôle</a>
    <% end %>
    <%= link_to 'Modifier', edit_complaint_path(id: @variables[:complaint].id, search: @params[:search]), class: 'uk-button uk-button-primary' %>
    <%= link_to 'Supprimer', complaint_path(@variables[:complaint], search: @params[:search]), method: :delete, data: {confirm: 'Êtes-vous sûr ?'}, class: 'uk-button uk-button-danger' %>
  </div>
</div>

这是一个screen of my page,当它实际加载时。只是简单的数据展示

我不知道我是否提供了足够的信息,但提前感谢您的帮助

【问题讨论】:

  • 你能提供完整的堆栈跟踪错误吗?我怀疑你的观点有错误 ``` 10: ``` 或者你的 result控制器可能会吐出 nil 值?
  • 该错误意味着@variables 为nil,因此您无法在其上调用[] 方法。当你这样做时@variables[:complaint] 它也只是一个方法调用——你也可以这样写:@variables.[](:complaint). Any idea, why @variables could be empty? My only guess it, that it should be redirect_to 投诉路径 && return` 在你的控制器中,但我不确定。

标签: ruby-on-rails ruby


【解决方案1】:

我认为您的应用程序在 2 台服务器上运行,由负载均衡器分担负载。 我认为是因为以下两种情况:

  1. 见行号。您的日志中的 9 个,即
 9:  <%# <%= link_to 'Retour à l\'interaction', interaction_path(@variables[:complaint].interaction, search: @params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>

并且您的 html 文件不包含此代码,这是 show.html 中的一些更改,即

<%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: @params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
  1. 页面刷新时出现此错误,表示1个服务器包含正确代码,1个包含错误代码,因此当请求在服务器1上时,它会显示页面,当它在服务器2上时,它会出错。李>

根据日志,我认为complaint.kind 是空的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    相关资源
    最近更新 更多