简体   繁体   English

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

[英]Ruby On Rails - empty object reference? (newbie)

I'm new to Ruby on Rails, but has quite some experience in other OO programming. 我是Ruby on Rails的新手,但是在其他OO编程方面有相当的经验。

Has got stuck though - it seems - when trying to create a simple blog application following this guide: http://guides.rubyonrails.org/getting_started.html 但是-似乎-尝试按照此指南创建简单的博客应用程序时遇到了麻烦: http : //guides.rubyonrails.org/getting_started.html

In point 5.7 (Showing Posts), the values from the form should be displayed on screen, but my display is just empty: 在第5.7点(显示帖子)中,表单中的值应显示在屏幕上,但我的显示为空:

Title: 标题:

Text: 文本:

??? ???

Would really appreciate any tips to where the error might be ! 真的很感谢任何提示错误的地方!

... don't know if the code below is sufficient to narrow it down, otherwise let me know of course. ...不知道下面的代码是否足以缩小范围,否则请让我知道。

Thank you very much for any help! 非常感谢您的帮助!

/Johan /约翰


My show.html.erb file is simply: 我的show.html.erb文件很简单:

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

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

but seems that @post reference an empty object? 但似乎@post引用一个空对象?

-- -

Here is my posts_controller.rb file: 这是我的posts_controller.rb文件:

class PostsController < ApplicationController def new end 类PostsController <ApplicationController def新端

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 

end 结束

(the render text: command displays the parameters correctly) (render text:命令正确显示参数)

-- -

In your create action you should have: 在创建操作中,您应该具有:

@post = Post.new(post_params)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM