首页 文章

Ruby On Rails - 空对象引用? (新手)

提问于
浏览
0

我是Ruby on Rails的新手,但在其他OO编程方面有相当丰富的经验 .

虽然看起来很困难 - 但是在尝试根据本指南创建一个简单的博客应用程序时:http://guides.rubyonrails.org/getting_started.html

在5.7(显示帖子)中,表格中的值应显示在屏幕上,但我的显示只是空的:

Headers :

文本:

???

非常感谢错误可能出现的任何提示!

...不知道下面的代码是否足以缩小范围,否则请告诉我 .

非常感谢您的帮助!

/约翰


我的show.html.erb文件很简单:

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

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

但似乎@post引用了一个空对象?

这是我的posts_controller.rb文件:

class PostsController <ApplicationController def new end

def create
    @post = Post.new(post_params[:post])

    @post.save
    #render text: params[:post].inspect
    redirect_to @post   
end

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

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

结束

(render text:命令正确显示参数)

1 回答

  • 0

    在您的创建操作中,您应该:

    @post = Post.new(post_params)
    

    而不是 @post = Post.new(post_params[:post])

相关问题