我正在尝试让我的 Posts 控制器工作,但我不断收到以下错误消息:
undefined method 'each' for `#<Post:0x000000062820c0>`
and : match ? attribute_missing(match, *args, &block) : super
我刚刚包含了属性方法,但错误仍然相同。我在下面发布我的控制器文件。谢谢您的帮助。
class PostsController < ApplicationController
include ActiveModel::AttributeMethods #Just added this..the error message was the
#same without it.
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post #.find(params[:id])
else
render :new
end
end
def show
@post = Post.find(params[:id]) # show
end
private
def post_params
params.require(:Title).permit(:Body, :url)
end
end
这是展示视图:
<h1>Posts</h1>
<% @post.each do |post| %>
Title: <%= post.Title %><br>
Body: <%= post.Body %><br>
url: <%= post.url %><br>
<% end %>