无法自动加载常量 POST,预期 /example/app/models/post.rb 来定义它



我必须做一个可以评论其他评论的评论,我在这里按照本教程进行操作,但是当我尝试运行应用程序程序时,会出现以下错误:

Unable to autoload constant POST, expected /example/app/models/post.rb to define it

提取的源(围绕第 #79 行(:

def find_commentable
@commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
@commentable = POST.find_by_id(params[:post_id]) if params[:post_id]
end

而且我真的不明白如果具有帖子模型(并且是这样的(,为什么会出现此错误:

模特帖子:

class Post < ApplicationRecord
belongs_to :city
has_many :comments, as: :commentable
end

如果您需要,这是注释模型:

class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
has_many :comments, as: :commentable
end

这是评论的控制器:

class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :find_commentable
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
@comment = @commentable.comments.new comment_params
#@comment = Comment.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: @comment }
else
format.html { render :edit }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:publication, :author, :content, :reputation, :creation_date)
end
def find_commentable
@commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
@commentable = POST.find_by_id(params[:post_id]) if params[:post_id]
end
end

我真的不知道为什么程序无法自动加载 Post。

您需要在#find_commentable内将POST.find_by_id更改为Post.find_by_id

相关内容

最新更新