gpt4 book ai didi

ruby-on-rails - Ruby on Rails 错误 :AssociationRelation is not an ActiveModel-compatible object. 它必须实现:to_partial_path

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:12 25 4
gpt4 key购买 nike

我有以下看法:

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

<%= will_paginate %>

<%= render [@answers]%>

<%= will_paginate %>

以及以下 index 操作:

def index
@question = Question.find(params[:question_id])
@answers = @question.answers.paginate(page: params[:page])
respond_with(@answers)
end

我收到以下错误:

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.

我该如何解决?

最佳答案

您可以通过两种不同的方式进行。

<%= render @answers %>  OR  
<%= render partial: 'answer', collection: @answers %>

在这两种情况下,您都会在部分 View 中获得一个名为 answer 的对象。

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

关于ruby-on-rails - Ruby on Rails 错误 :AssociationRelation is not an ActiveModel-compatible object. 它必须实现:to_partial_path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com