【问题标题】:Why am I getting 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path error?为什么我得到“nil”不是 ActiveModel 兼容的对象。它必须实现 :to_partial_path 错误?
【发布时间】:2013-11-05 08:34:49
【问题描述】:

为什么会出现以下错误?

nil 不是 ActiveModel 兼容的对象。它必须实现 :to_partial_path。

我认为该错误可能与我正在使用 Rails 3.2 而我正在使用 Rails 4 的教程有关。

这是型号代码:

class DashboardsController < ApplicationController
  def show
    @text_shout = TextShout.new
    @photo_shout = PhotoShout.new
    @shouts = current_user.shouts
  end
end

class PhotoShoutsController < ApplicationController
  def create
    content = build_content
    shout = current_user.shouts.build(content: content)
    if shout.save
      redirect_to dashboard_path
    else
      flash.alert = "Could not shout."
      redirect_to dashboard_path
    end
  end

  private
  def build_content
    PhotoShout.new(photo_shout_parameters)
  end

  def photo_shout_parameters
    params.require(:photo_shout).permit(:image)
  end
end

这是 _shout.html 部分发生错误的视图代码

# app/view/dashboards/show.html.erb
<%= form_for @text_shout do |form| %>
   <%= form.text_field :body, placeholder: 'Shout content here' %>
   <%= form.submit 'Shout' %>
<% end %>

<%= form_for @photo_shout do |form| %>
   <%= form.file_field :image %>
   <%= form.submit 'Shout' %>
<% end %>

<%= render @shouts %>

# app/view/shouts/_shout.html.erb
<%= div_for shout do %>
  <%= link_to shout.user.username, shout.user %>
  shouted
                                 +---------------------------------+
  <%= render shout.content %> <--| ERROR "nil' is not an Active "  |
                                 | "Model-compatible object"       |
                                 +---------------------------------+
  <%= link_to time_ago_in_words(shout.created_at), shout %>
<% end %>

# app/views/photo_shouts/_photo_shout.html.erb
<%= image_tag photo_shout.image.url(:shout) %>

【问题讨论】:

  • 您在哪一行收到此错误??

标签: ruby-on-rails polymorphism renderpartial


【解决方案1】:

将 ActiveModel 包含到普通的 Ruby 对象中

Rails 4 中的 Thoughtbot 中级教程几乎没有复杂性,但是,将 ActiveModel 功能添加到普通的 Ruby 对象中会遇到一个问题。我在第 3 周的视频中看到这一点,大约半小时左右,而导师(AKA Mr Halogenandtoast)正在提取时间线对象。

而不是:扩展 ActiveModel::Naming 您想要包含 ActiveModel::Model - Rails 4 进行了更改,使其更容易处理普通对象。

class Timeline
  include ActiveModel::Model
...
...

For Thoughtbot learn subscribers there's a link to the discussion. 强参数是本教程的另一个问题。

【讨论】:

    【解决方案2】:

    您遇到的问题是因为您的数据库中有现有记录没有与之关联的内容。发生这种情况是因为您从非多态设置变为多态设置。您需要做的是查找缺少 content_type 和 content_id 的呼喊并将它们从数据库中删除。删除这些后,添加可能会很有用

    验证关联:内容

    到您的 Shout 模型,以确保将来的数据不会“破坏”您的数据库。

    【讨论】:

    • 在 Rails 控制台中,我用 Shout.destroy_all 删除了所有的喊声。 >>>Shout.destroy_all => [#]。我还添加了验证,但 photoshout 没有保存,我被重定向到仪表板,并显示 Flash 消息“不能喊”。
    • 所以看起来您可能没有正确传递内容值。您可以在提出请求时尝试粘贴您的日志吗?
    • 我的日志给了我 imagemagick 错误。所以,我“brew uninstall imagemagick”是因为我有一个过时的版本并重新安装了它。我还将 Paperclip.options[:command_path] = "/usr/local/bin/" 放在我的 Rails config/environment/development.rb 中。谢谢。
    • 如果您在 Rails 4 中而不是推荐的 3.X 中遵循此问题,那么您将在第三个视频的 30 分钟左右遇到此问题(这是我遇到的地方)。简而言之,而不是:扩展 ActiveModel::Naming 您想要包含 ActiveModel::Model - 或查看我的完整答案。
    【解决方案3】:

    @shouts = current_user.shouts 在这条线上你的@shouts 设置为nil

    检查current_user.shouts,它必须返回为零

    编辑
    试试这个

    &lt;%= render @shouts.content %&gt;

    【讨论】:

    • shout.html.erb 部分渲染来自哪个动作??
    • 我有一个多态关联,其中喊叫包含 content_id 和 content_type。 DashboardsController 显示包含 @shouts = current_user.shouts。我回答你的问题了吗?
    • 我试过 它给了我--- nil:NilClass 的未定义方法`content'
    • 顺便说一下,感谢您的帮助。
    • 不,你没有从你的show.html.erb 文件中调用部分,因为@shout 是在show 操作中定义的。看这很简单,你需要设置@shout 变量控制器在您调用部分的同一操作中
    【解决方案4】:

    我在开发日志中发现了这个错误,这一直是问题所在。我超级困惑了一会儿。

    [paperclip] An error was received while processing <Paperclip::Errors::CommandNotFoundError: Could not run the `identify` command. Please install ImageMagick.>
    

    看起来修复只是运行brew update(可选)和brew install imagemagick,其他任何人都在寻找思想机器人教程的修复。

    【讨论】:

      【解决方案5】:

      安装 imagemagick 将解决这个问题。

      检查自述文件中的要求部分

      https://github.com/thoughtbot/paperclip#requirements https://github.com/thoughtbot/paperclip#image-processor

      【讨论】:

        【解决方案6】:

        brew install imagemagick 成功了。

        即使在清除了所有旧记录的数据库之后,我也遇到了类似的错误。 PhotoShout 使用content_id: nil 保存到数据库中,这显然是问题的根源。

        我再次清理数据库,运行brew install imagemagick,照片开始上传成功。

        【讨论】:

          猜你喜欢
          • 2018-08-04
          • 1970-01-01
          • 2015-05-14
          • 1970-01-01
          • 2016-05-09
          • 2020-03-07
          • 2013-08-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多