解決法
posts_controller.rb
def index
@posts = Post.all
end
index.html.erb
<%= form_for @posts ,html: { class: "form-group" } do |form| %>
@posts = Post.all
で定義しているインスタンス変数に対してform_for
を使っていたため、form_for
を使用しない構成にしました。
原因
■ ActiveRecord::Relationを返すメソッド
all
scope (実装でnilを返すとallと同様になる)
select (使い方次第)
group
order
reorder
unscope
from
:
(省略)
:
■ 配列やオブジェクトを返すメソッド
select (使い方次第)
find
find_by
take
first
last
second
third
fourth
fifth
:
(省略)
:
メソッドにはActiveRecord::Relationを返す
ものと配列やオブジェクトを返すメソッド
と種類があり、form_for
の対象であるモデルをall
であるActiveRecord::Relationを返すメソッド
を使用していたためにエラーが発生したものと思われます。