0

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

虽然卡住了 - 似乎 - 当尝试按照本指南创建一个简单的博客应用程序时:http: //guides.rubyonrails.org/getting_started.html

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

标题:

文本:

???

非常感谢任何提示错误可能在哪里!

...不知道下面的代码是否足以缩小范围,否则当然让我知道。

非常感谢您的帮助!

/约翰


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

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

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

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

--

这是我的 posts_controller.rb 文件:

类 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 

结尾

(渲染文本:命令正确显示参数)

--

4

1 回答 1

0

In your create action you should have:

@post = Post.new(post_params)

instead of @post = Post.new(post_params[:post])

于 2014-01-18T12:02:00.100 回答