Having trouble with Getting Started with Rails guide while using ruby2.0.0 with rails 4.0

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?

On Thursday, September 12, 2013 10:06:57 PM UTC+1, Derek Nassler wrote:

… 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:

Your show action isn’t being called because the method is private (and
actions have to be public methods). Rails is skipping straight to the
template. Post.find will always return something or raise an exception -
it
won’t return nil

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs