【问题标题】:nil' is not an ActiveModel-compatible object. It must implement :to_partial_pathnil' 不是 ActiveModel 兼容的对象。它必须实现 :to_partial_path
【发布时间】:2018-08-04 13:17:16
【问题描述】:

尝试运行我的 Rails 应用时出错。我认为这可能是基于我在 Stack Overflow 中研究的类似错误的数据问题。下面是命令行的输出:

ActionView::Template::Error ('nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.):
2: <% provide(:blog_active, 'active') %>
3:
4: <div class="row">
5:   <%= render @posts %>
6: </div>

app/views/blog/posts/index.html.erb:5:in `_app_views_blog_posts_index_html_erb__817338750_58788108'

【问题讨论】:

  • 您可以添加&lt;% p ['@posts is', @posts] %&gt; 并在您的控制台上查看是否为 nil 吗?
  • 菲利普,谢谢你的建议;我到底在哪里插入这段代码?再次感谢。
  • 就在您的&lt;div class="row"&gt;上方
  • 很酷,谢谢...现在试试。
  • 得到以下结果: ActionView::Template::Error ('nil' 不是 ActiveModel 兼容的对象。它必须实现 :to_partial_path。): 2: 3: 4:
    5: 6:
    app/views /blog/posts/index.html.erb:5:in `_app_views_blog_posts_index_html_erb__299498262_61543404'

标签: ruby-on-rails


【解决方案1】:

可能与您的部分名称存在差异。你确定是 _posts.html.erb 而不是 _post.html.erb?

我刚刚发现了这个相关的 Stack Overflow 问题: Rails rendering instance variable from application.html.erb

【讨论】:

  • 感谢您抽出宝贵时间提供答案。正是因为像您这样乐于助人的同龄人,我们才能作为一个社区一起学习。以下是一些关于如何使您的答案出色的提示:How do I write a good answer
  • 是的,它被标记为 _post.html.erb...现在重新运行...手指交叉。
  • 将文件名更改为_posts.html.erb后出现同样的错误。
  • 你能弄明白吗?
  • Ashwin 感谢您的提问。还没有。我刚从头开始,无法在我的应用程序中显示图像。现在正在努力。我将发布我的求助请求。
【解决方案2】:

我有同样的错误。我的问题是我的 PostsController #index 中的 @posts 变量。我在编写 @posts 变量时犯了一个错误。 当我修复 @posts 值时,问题就消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多