Rails指南第5.7节中的NoMethodError

我正在关注Rails 4.0.0的入门教程: http : //guides.rubyonrails.org/getting_started.html

我正处于5.7节中我应该得到ActiveModel :: ForbiddenAttributes错误的地方。 相反,我得到这个错误:

NoMethodError in Posts#show Showing C:/Rails/blog/app/views/posts/show.html.erb where line #8 raised: undefined method `text' for nil:NilClass Extracted source (around line #8): 5 6 

7 Text: 8 9

尽管如此,我相信这些post正在创建,因为每次提交表单时ID都会增加。 我是Rails的新手,并试图完全按照说明操作。

我正在使用Ruby 1.9.3和Rails 4.0.0运行Windows 7 x64。

这是一些相关的文件; 如果需要其他任何其他信息,请告诉我。

posts_controller.rb:

 class PostsController < ApplicationController def new end def create @post = Post.new(post_params) @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 

show.html.erb:

 

Title:

Text:

new.html.erb

 

New Post



只需在create方法之后写下show方法,因为你的show方法低于关键字private它将私有作为Access Modifier ,因此不能直接通过浏览器访问

 class PostsController < ApplicationController def new end def create @post = Post.new(post_params) @post.save redirect_to @post end def show @post = Post.find(params[:id]) end private def post_params params.require(:post).permit(:title, :text) end end 

我在教程中遇到了同样的问题(在这个日期(2014年11月18日)使用’文章’而不是’post’),并且发现解决方案是在articles_controller中放置以下“def”块。 RB:

  def show @article = Article.find(params[:id]) end 

这是我的样子:

 class ArticlesController < ApplicationController def new end def create @article = Article.new(article_params) @article.save redirect_to @article end def show @article = Article.find(params[:id]) end private def article_params params.require(:article).permit(:title, :text) end end