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.
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.
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:
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.
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:
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
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
Thank you.