1

我是 Rails 新手,正在阅读Ruby on Rails 入门教程。我在 5.7 Showing Posts 上遇到错误,上面写着'undefined method title' for nil:NilClass'. 您能提供的任何帮助将不胜感激。

class PostsController < ApplicationController
def new
end

def create
    @post = Post.new(params[:post].permit(:title, :text))

    @post.save
    redirect_to @post
end

private
def post_params
    params.require(:post).permit(:title, :text)
end

def show
    @post = Post.find(params[:id])
end

end



<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @post.text %>
</p>
4

2 回答 2

4

您的show方法是私有的,您需要将其移至关键字上方。

将来你可能更喜欢写

def some_method
  ...
end
private :some_method

避免这种情况。

于 2013-07-20T13:15:26.673 回答
0

谢谢,这对我有用。将“显示”定义移至私有块上方。我认为入门文档的顺序可能令人困惑。

于 2013-10-06T12:07:01.917 回答