SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    No Method Error in Stories#bin

    I am around page 329 when I noticed this error. It seems like it is not happy with my user login information. This error appears when I try to view the "upcoming stories" page.

    Thanks

    Code:
    NoMethodError in Stories#bin
    
    Showing app/views/stories/_story.html.erb where line #4 raised:
    
    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.login
    Extracted source (around line #4):
    
    1: <% div_for(story) do %>
    2: 	<h3><%= link_to story.name, story %></h3>
    3: 	<p>
    4: 		Submitted by: <%= story.user.login %> | 
    5: 		Score: <%= story.votes_count %>
    6: 	</p>
    7: <% end %>
    Trace of template inclusion: app/views/stories/index.html.erb
    
    RAILS_ROOT: /Users/slam/Code/shovell
    
    Application Trace | Framework Trace | Full Trace
    /Users/slam/Code/shovell/app/views/stories/_story.html.erb:4:in `_run_erb_app47views47stories47_story46html46erb_locals_object_story_story_counter'
    /Users/slam/Code/shovell/app/views/stories/_story.html.erb:1:in `_run_erb_app47views47stories47_story46html46erb_locals_object_story_story_counter'
    /Users/slam/Code/shovell/app/views/stories/index.html.erb:4:in `_run_erb_app47views47stories47index46html46erb'
    /Users/slam/Code/shovell/app/controllers/stories_controller.rb:24:in `bin'

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you look at the stack trace at the bottom it's leading you towards the problem:

    Code:
    /Users/slam/Code/shovell/app/controllers/stories_controller.rb:24:in `bin'
    stories_controller.rb - line 24, it's trying to load the bin method, but it's not finding it.

    What does your stories_controller.rb look like?

  3. #3
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Delameko View Post
    If you look at the stack trace at the bottom it's leading you towards the problem:

    Code:
    /Users/slam/Code/shovell/app/controllers/stories_controller.rb:24:in `bin'
    stories_controller.rb - line 24, it's trying to load the bin method, but it's not finding it.

    What does your stories_controller.rb look like?
    Code:
       def bin
         fetch_stories 'votes_count < 5'
         render :action => 'index'
       end  
       protected
       def fetch_stories(conditions)
         @stories = Story.find :all,
            :order => 'id DESC', 
            :conditions => conditions
       end
    That is the line 24 which is calling index. Index.rhtml is

    Code:
    <h2>
    	<%= story_list_heading %></h2>
    </h2>
    <%= render :partial => @stories %>

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it's my bad, I was in a hurry (had just finished work) and misinterpreted the error message.

    It's trying to load a method on line 4 of your _story.html.erb partial, that it can't find.

  5. #5
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Delameko View Post
    Actually, it's my bad, I was in a hurry (had just finished work) and misinterpreted the error message.

    It's trying to load a method on line 4 of your _story.html.erb partial, that it can't find.
    Thanks for the help. The code is in my first post. I checked it and it appears correct to me. Any other ideas?

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is that story.user is returning a nil object. When Ruby then looks for the method 'login' on the object returned by story.user it can't find that method - the nil object doesn't have one.

    Basically, the error means you don't have a user associated to the story. This could happen because of one of the following:

    1. No user has been assigned to the story
    2. The user that was assigned to the story has been deleted or altered in a way that broke the association.
    3. The code that associates users with with stories is broken.

    You could handle the problem by altering line 4 to:
    Code:
     Submitted by: <&#37;= story.user ? story.user.login : 'unknown' -%> |
    Alternatively add code to ensure that a user is always associated with a story.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •