NoMethodError in Stories#show

I’m a Rails newbie and am working my way through Simply Rails 2 by
Patrick Lenz.

Have run into a problem on page 207 of the book; this is the error I
run into when I fire up my server and open up httpL//localhost:3000/
stories/new I have posted the error message and trace below.

Thanks in advance for your help.


Showing app/views/stories/show.html.rb where line #1 raised:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name
Extracted source (around line #1):

1:

<%= @story.name %>


2:

<%= link_to @story.link, @story.link %>


RAILS_ROOT: /Users/pdenlinger/Sites/In_Development/shovell

Application Trace | Framework Trace | Full Trace
app/views/stories/show.html.rb:1
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/
renderable.rb:39:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/ renderable.rb:39:inrender’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/
template.rb:73:in render_template' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb: 256:inrender’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb:
367:in _render_with_layout' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_view/base.rb: 254:inrender’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
base.rb:1174:in render_for_file' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ base.rb:896:inrender_without_benchmark’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/ core_ext/benchmark.rb:8:inrealtime’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ base.rb:868:inrender_without_benchmark’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/ core_ext/benchmark.rb:8:inrealtime’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ base.rb:1248:indefault_render’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
base.rb:1259:in perform_action_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ filters.rb:617:incall_filters’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
filters.rb:610:in perform_action_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ benchmarking.rb:68:inperform_action_without_rescue’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
benchmarking.rb:68:in perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ rescue.rb:136:inperform_action_without_caching’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
caching/sql_cache.rb:13:in perform_action' /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/ connection_adapters/abstract/query_cache.rb:34:incache’
/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
query_cache.rb:8:in cache' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ caching/sql_cache.rb:12:inperform_action’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
base.rb:524:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ base.rb:524:inprocess_without_filters’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
filters.rb:606:in process_without_session_management_support' /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/ session_management.rb:134:inprocess’
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
base.rb:392:in process' /Library/Ruby/Gems/1.8/gems/rails-2.2.2/lib/webrick_server.rb:74:inservice’
/Library/Ruby/Gems/1.8/gems/rails-2.2.2/lib/commands/servers/
webrick.rb:66
/Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/
dependencies.rb:153:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/ dependencies.rb:521:innew_constants_in’
/Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/active_support/
dependencies.rb:153:in `require’
/Library/Ruby/Gems/1.8/gems/rails-2.2.2/lib/commands/server.rb:49

On Tue, Sep 29, 2009 at 6:32 PM, [email protected]
[email protected] wrote:


Showing app/views/stories/show.html.rb where line #1 raised:

In your “stories” controller, in your “show” method, are instantiating
a @story object? Do you see something like

@story = Story.find(…)

?

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name
Extracted source (around line #1):

1:

<%= @story.name %>

Cause down here in your view you seem to be expecting one.


Greg D.
http://destiney.com/

2009/9/30 [email protected] [email protected]:


Showing app/views/stories/show.html.rb where line #1 raised:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name
Extracted source (around line #1):

1:

<%= @story.name %>

The error means that when it tries to display @story.name then @story
is nil. This likely means that the find used to setup @story in your
controller did not find a record.

I suggest having a look at the rails guide on Testing Rails Apps at
http://guides.rubyonrails.org/ This may help you to work out what is
wrong. In addition the Getting Started guide and ActiveRecord
Associations are compulsory reading.

Colin

Yes, I do have a method in the stories_controller.rb file. I have
copied the contents of the file below:

class StoriesController < ApplicationController
def index
@story = Story.find(:first, :order => ‘RANDOM()’)
end
def new
@story = Story.new
end
def create
@story = Story.new(params[:story])
if @story.save
flash[:notice] = ‘Story submission succeeded’
redirect_to stories_path
else
render :action => ‘new’
end
def show
@story = Story.find(params[:id])
end
end
end

Following is the content of the show.html.rb file:

<%= @story.name %>

<%= link_to @story.link, @story.link %>

Still not quite sure what is wrong.

Thanks!

if @story.save

Following is the content of the show.html.rb file:

<%= @story.name %>

<%= link_to @story.link, @story.link %>

Well if you are using routes like: <% map.resources :stories %>
Then your show url should be like <% http://[HTTP_HOST]/stories/1/ %>
with HTTP GET method.
If you are using ajax request then make sure you set the HTTP method
to GET in your <% link_to_remote %> method.

And if you are not using routes then Manish already indicated a
possibility :smiley:

Over all make sure you are getting the story id in your params[:id]
variable in show action.

Hope you will be able to find out the problem :slight_smile:
Thank you.

Samiron paul
http://samironpaul.blogspot.com
http://www.scrumpad.com

Hey paul

can u paste your show url.

bcoz in your show method you have write

def show
@story = Story.find(params[:id])
end

your url must be…
http//localhost:3000/stories/show/id

so please paste your show url where you get error.

Colin L. wrote:

2009/9/30 [email protected] [email protected]:


Showing app/views/stories/show.html.rb where line #1 raised:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name
Extracted source (around line #1):

1:

<%= @story.name %>

The error means that when it tries to display @story.name then @story
is nil. This likely means that the find used to setup @story in your
controller did not find a record.

I suggest having a look at the rails guide on Testing Rails Apps at
http://guides.rubyonrails.org/ This may help you to work out what is
wrong. In addition the Getting Started guide and ActiveRecord
Associations are compulsory reading.

Colin

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