ここにある 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を初めて使用し、指示に正確に従おうとしました。
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:
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>
<strong>Text:</strong>
<%= @post.text %>
</p>
new.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 %>