paul.denlinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Sep-29 23:32 UTC
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: <h2><%= @story.name %></h2> 2: <p><%= link_to @story.link, @story.link %></p> 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:in `render'' /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:in `render'' /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:in `render'' /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:in `render_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:in `realtime'' /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:in `render_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:in `realtime'' /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:in `default_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:in `call_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:in `perform_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:in `perform_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:in `cache'' /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:in `perform_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:in `process_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:in `process'' /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:in `service'' /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:in `new_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, paul.denlinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <paul.denlinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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. -- Greg Donald http://destiney.com/
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! On Sep 30, 8:29 am, Greg Donald <gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Sep 29, 2009 at 6:32 PM, paul.denlin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > <paul.denlin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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. > > -- > Greg Donaldhttp://destiney.com/
2009/9/30 paul.denlinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <paul.denlinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > 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: <h2><%= @story.name %></h2>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:> 2009/9/30 paul.denlinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <paul.denlinger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> -------------------------------------------------------------------------------------- >> >> 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: <h2><%= @story.name %></h2> > > 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-- Posted via http://www.ruby-forum.com/.
> > 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 :D 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. Samiron paul http://samironpaul.blogspot.com http://www.scrumpad.com