【问题标题】:Rails Ajax Internal Server ErrorRails Ajax 内部服务器错误
【发布时间】:2014-01-08 18:49:42
【问题描述】:

我正在通过 ajax 加载我的应用程序的一部分,我已经全部设置好了,我点击了上面设置了 remote: true 的链接,请求了正确的 URL,但在控制台中我收到了 500 错误并且以下:

在 2014-01-08 18:46:08 开始 GET "/coasters?page=18" for 127.0.0.1 +0000 CoastersController#index 作为 JS 参数处理:{"page"=>"18"} 渲染共享/_fact-bar.html.erb (0.9ms) 渲染 Coasters/index.html.erb (1.5ms) Completed 500 Internal Server Error in 3毫秒

NoMethodError - 未定义的方法 each_with_index' for nil:NilClass:
app/views/shared/_fact-bar.html.erb:6:in
_app_views_shared__fact_bar_html_erb__2012545158101504906_70360428754860' actionpack (3.2.12) lib/action_view/template.rb:145:in block in render' activesupport (3.2.12) lib/active_support/notifications.rb:125:ininstrument' actionpack (3.2.12) lib/action_view/template.rb:143:in `render'

我不太确定是什么部分导致了错误。有帮助吗?

事实栏部分:

<div class="facts bar bar--white-wave">
  <div class="container--padded">
    <h1>Facts &amp; Statistics</h1>
    <ul class="fact-circle-container">
      <% circleCount = 1 %>
      <% @stats.each_with_index do |stat, i| %>
        <% if stat[:active] == 1 then %>
          <li>
            <% if stat[:link] then %>
              <a href="<%= stat[:link] %>">
            <% end %>
              <div class="fact-circle fact-circle--<%= circleCount %>" data-number="20">
                <div class="inner">
                  <span><%= stat[:value] %></span>
                </div>
              </div>
            <% if stat[:link] then %>
              </a>
            <% end %>
            <h3 class="fact-circle__title"><%= stat[:name] %></h3>
          </li>
          <% circleCount = circleCount + 1 %>
        <% end %>
      <% end %>
    </ul>
  </div>
</div>

【问题讨论】:

  • 你能告诉我们fact-bar.html.erb部分吗?看起来您可能正在对非 Enumerable 类型的东西调用 each_with_index。
  • 您在 _fact-bar 中调用 each_with_index,部分内容是 nil
  • 你把所有的东西都写在那里了:文件 app/views/shared/_fact-bar.html.erb,第 6 行。你试图在返回 nil 的变量上调用 each_with_index。
  • 将事实栏部分添加到我的 OP。我不知道为什么这会导致错误,因为该部分与我正在 ajaxing 的部分无关?
  • 需要显示设置它的控制器操作,也许在第 18 页上没有更多结果?

标签: ruby-on-rails ajax


【解决方案1】:

发生错误是因为您在 @stats 上调用 each_with_index,在您的部分中其计算结果为 nil

大概,您的 AJAX 操作没有以 html 格式调用 index - 这是定义 @stats 的位置。因此,您需要重构定义 @stats 的代码,以便从您的 ajax 操作/格式中调用。

【讨论】:

  • 好的。等我回来再看看
  • 要么将 @stats 声明放在 respond_to 块之前,要么将其放在可以在需要它的每种格式中调用的私有方法中。
  • 布里尔。刚刚开始工作。我有另一个关于分页的问题,但最好是一个新问题,所以会这样做。再次感谢!
猜你喜欢
  • 2017-04-12
  • 2020-02-12
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 2018-05-23
相关资源
最近更新 更多