Ruby on rails 未定义的方法“find#u post';对于#<;PostsController:0x000010B791560>;
这就是我被困在代码中的原因:Ruby on rails 未定义的方法“find#u post';对于#<;PostsController:0x000010B791560>;,ruby-on-rails,Ruby On Rails,这就是我被困在代码中的原因: class PostsController < ApplicationController before_action :find_post, only: [:show, :edit, :update, :destroy] def index end def show end def new @post = Post.new end def create @post = Post.new(post_pa
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
def index
end
def show
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def edit
end
def update
if @post.update
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def post_find
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content)
end
end
class PostsController
我定义了post_find,但在运行代码时仍然会出错。对于post中的错误,我很抱歉,我是rails新手。我希望能够在论坛上发表文章,并进行编辑或删除文章。应该是这样的
def find_post
@post = Post.find(params[:id])
end
您将回调定义为
:find\u post
,但方法定义是post\u find
它应该是:post\u find
类似的
before_action :post_find, only: [:show, :edit, :update, :destroy]
def post_find
@post = Post.find(params[:id])
end
因为您的方法是post\u find
def post_find
@post = Post.find(params[:id])
end
我想对你有帮助你能解释一下你的应用程序的上下文并添加错误吗?你定义了一个方法post\u find,但试图调用find\u post