我正在关注位于此处的 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 <p>
7 <strong>Text:</strong>
8 <%= @post.text %>
9 </p>
尽管如此,我相信帖子正在创建,因为每次我提交表单时,id 都会增加。我是 Rails 的新手,并试图完全按照说明进行操作。
我正在运行 Windows 7 x64,带有 Ruby 1.9.3 和 Rails 4.0.0。
以下是一些相关文件;请让我知道是否需要其他任何内容。
post_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
显示.html.erb:
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>
<strong>Text:</strong>
<%= @post.text %>
</p>
新的.html.erb
<h1>New Post</h1>
<%= form_for :post, url: posts_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>