Ruby on Rails 错误:AssociationRelation 不是与 ActiveModel 兼容的对象。它必须实现 :to_partial_path

vox*_*ter 3 ruby ruby-on-rails

我有以下观点:

# index.html.web
<h1>Listing answers</h1>

<%= will_paginate %>

  <%= render [@answers]%>

<%= will_paginate %>
Run Code Online (Sandbox Code Playgroud)

以及以下index操作:

# index.html.web
<h1>Listing answers</h1>

<%= will_paginate %>

  <%= render [@answers]%>

<%= will_paginate %>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

ActiveRecord::AssociationRelation [#<Answer id: 2, content: "b", user_id: nil, question_id: 1, created_at: "2015-04-27 14:59:32", updated_at: "2015-04-27 14:59:32">, #<Answer id: 3, content: "d", user_id: 1, question_id: 1, created_at: "2015-04-27 15:15:01", updated_at: "2015-04-27 15:15:01"] is not an ActiveModel-compatible object. It must implement :to_partial_path.
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

Sar*_*mar 5

你可以用两种不同的方式来做到这一点。

<%= render @answers %>  OR  
<%= render partial: 'answer', collection: @answers %>
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,您都会得到一个以answer局部视图命名的对象。

并且您必须_answer.html.erb在同一目录中存在一个名为should的文件。