Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法“find#u post';对于#<;PostsController:0x000010B791560>;_Ruby On Rails - Fatal编程技术网

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