About midway through the Getting Started with Rails guide
(http://guides.rubyonrails.org/getting_started.html) I ran into a
problem.
The problem is in section: “5.7 Showing Posts”.
The document describes how to setup the “show.html.erb” file as follows:
Title: <%= @post.title %>
Text: <%= @post.text %>
and the associated controller looks like:
class PostsController < ApplicationController
def new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
#redirect_to action: :show, id: @post.id
#render text: params[:post].inspect
end
private
def post_params
params.require(:post).permit(:title, :text)
end
def show
@post = Post.find(params[:id])
end
end
… show the “show” action is supposed to return the “post” information
(i.e. the blog post into the posts table) and I know that it exists in
the
table but I get an error indicating that:
NoMethodError in Posts#show
Showing *
/Users/dnassler/dev/rails/workspace/blog/app/views/posts/show.html.erb*
where
line #3 raised:
undefined method `title’ for nil:NilClass
Extracted source (around line #3):
1
2
3
4
5
6
Title: <%= @post.title %>
Rails.root: /Users/dnassler/dev/rails/workspace/blog
Application Trace http://localhost:3000/posts/8# | Framework
Tracehttp://localhost:3000/posts/8#
| Full Trace http://localhost:3000/posts/8#
app/views/posts/show.html.erb:3:in
`_app_views_posts_show_html_erb___2211649412857058384_70196496456240’
Request
Parameters:
{“id”=>“8”}
So it is as if the Post.find(params[:id]) call within the “show” method
failed to find the post with id=8? Or maybe there is something wrong
with
the way that the show.html.erb file is defined?