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.

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:

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: <h2><%= @story.name %></h2>

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

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:

<h2><%= @story.name %></h2> <p><%= link_to @story.link, @story.link %></p>

Still not quite sure what is wrong.

Thanks!

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

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 Law wrote:

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:

<h2><%= @story.name %></h2> <p><%= link_to @story.link, @story.link %></p>

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