rubyのactive hashを活用して地域のプルダウンを作成し、DB(mysql)の保存は確認できましたが、保存されたデータをviewファイルに表示しようとした際にエラーになります。
index.html.erb
<div class="contents row"> <% @posts.each do |post| %> <td> <%= post.mark %></td><br> <td> <%= post.content %></td><br> <td> <%= post.area1.name %></td><br> <td> <%= post.area2.name %></td><br> <% end %> </div>post_controller
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :index]
def new
@post = Post.new
end
def index
@posts = Post.all
end
def create
post = Post.new(post_params)
if post.save
redirect_to root_path
else
render :new
end
end
private
def post_params
params.require(:post).permit(:image, :video, :area1_id, :area2_id, :mark, :injury_id, :escape_id, :help_id, :content).merge(user_id: current_user.id)
end
end
エラー文
NoMethodError in Posts#index
undefined method `area2' for #Post:0x00007f908c337858
Did you mean? area1
area1=
ちなみにarea2は存在してデータの保存もできています
<td> <%= post.mark %></td><br> <td> <%= post.content %></td><br> <td> <%= post.area1.name %></td><br> <td> <%= post.area2.name %></td><br>←こちら
ここの post.area2.name のところでエラーになります
ちなみにarea1.nameは一覧画面に表示されています。
原因が全く話からずなので、どなたか教えていただきたいです

回答1件
あなたの回答
tips
プレビュー