Ruby on Rails Thursday, September 12, 2013

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:

<p>
<strong>Title:</strong>
<%= @post.title %>
</p>

<p>
<strong>Text:</strong>
<%= @post.text %>
</p>

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            
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>

Rails.root: /Users/dnassler/dev/rails/workspace/blog

Application Trace | Framework Trace | Full Trace
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?


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/373af512-6d8c-47b9-ba5c-26b4ea17d052%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment