SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Rails 2 Error - NoMethodError in Stories#show

    Working through the book and running into an error on page 283. (not able to cut & paste in everything as forum is giving me an error about controlling spam by not allowing anyone with under 10 posts to post links or email)

    input text from pg 283 to show name of user who submitted story.
    Line 18 is the span statement with ...story.user.login

    Error:

    NoMethodError in Stories#show

    Showing where line #18 raised:

    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.login

    Using Instant Rails 2, Vista, Mongrel, Netbeans 6.5

    Thanks,

    Veronica

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Do your files look similar to these?

    show.html.erb
    Code Ruby:
    <h2>
      <span id="vote_score">
        Score: <%= @story.votes.size %>
      </span>
      <%= @story.name %>
    </h2>
    <ul id="vote_history">
      <% if @story.votes.empty? %>
        <em>No shoves yet!</em>
      <% else %>
        <%= render :partial => 'votes/vote',
            :collection => @story.votes.latest %>
      <% end %>
    </ul>
    <p class="submitted_by">
      Submitted by: <span><%= @story.user.login %></span>
    </p>
    <p>
      <%= link_to @story.link, @story.link %>
    </p>
    <div id="vote_form">
      <% form_remote_tag :url => story_votes_path(@story) do %>
        <%= submit_tag 'shove it' %>
      <% end %>
    </div>

    stories_controller.rb
    Code Ruby:
    class StoriesController < ApplicationController
     
      before_filter :login_required, :only => [ :new, :create ]
     
      def index
         @story = Story.find(:first, :order => 'RANDOM()')
      end
      def new
        @story = Story.new
      end
      def create
        @story = @current_user.stories.build params[:story]
        @story.save
        if @story.save
          flash[:notice] = 'Story submission succeeded'
          redirect_to stories_path
        else
          render :action => 'new'
        end      
      end
      def show
        @story = Story.find(params[:id])
      end
    end

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, exactly.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vhenry View Post
    Line 18 is the span statement with ...story.user.login
    Does the line look like story.user.login or @story.user.login?

    The latter is the correct way.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it's coded with the @ sign.

  6. #6
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not realated to your problem, but it's a bit of a bad habit to write @current_user and not user a method instead.
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hmm...maybe you could post the trace that pops up with the error. Hopefully we can get you to 10 posts so that we can see your code as well.

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I've been trying to figure out why @story.user would be nil, but I'm afraid I get a bit lost in the app.

    If you check the records in the database, eg.
    Code:
    >ruby script/console
    Loading development environment (Rails 2.1.0)
    >> s = Story.find(:all)
    => [#<Story id: 1,
     name: "My shiny weblog",
     link: "http://poocs.net/",
     created_at: "2008-08-30 13:31:03",
     updated_at: "2008-08-31 15:46:04",
     user_id: 1>,
     #<Story id: 2,
     name: "SitePoint Forums",
     link: "http://www.sitepoint.com/forums/",
     created_at: "2008-08-30 13:34:04",
     updated_at: "2008-08-31 15:46:39",
     user_id: 1>]
    Each Story has an id and a user_id?

  9. #9
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error means that your application is returning a nil for story.user. Have a look at the database and see what user id is given in the user_id field in the story you are trying to view. If there is no entry, then that is your problem, and you need to check the code that is adding or updating the user_id.

    If the story has an id number in user_id, then the next thing to check is that there is an User with that id. You would get this error if the user has been deleted that was associated with this story. If this is the error you may want to look at adding code that handles the situation where users are deleted and their stories are not.

    If there is a user_id and a matching user with that id, then it is probably the table association that is at fault. That is, a problem associated with the belongs_to :user statement. For example, you will get an error if your user table is called "user" rather than "users".

    It is always worth trying these sort of thing out at the console. You can access this via:
    Code:
    ruby script/console
    From the root of your application. Once in you can check that a story (e.g. 1) has a user (e.g. 3) like this:
    Code:
    s = Story.find(1)
    puts s.story_id
    u = User.find(3)
    puts "Match" if s.user == u

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems that each of my 4 stories does not have a userid, user_id is nil. I'm not sure exactly in which file during the tutorial I was supposed to set this up.

    I'll have to flip back through to see where I screwed things up...of course if someone has an idea, I'm all ears.

    Thanks for the help

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like the application trace and framework traces both have links, so no go there...but with this post, I think I'm up to 9

  12. #12
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vhenry View Post
    It seems that each of my 4 stories does not have a userid, user_id is nil. I'm not sure exactly in which file during the tutorial I was supposed to set this up.

    I'll have to flip back through to see where I screwed things up...of course if someone has an idea, I'm all ears.

    Thanks for the help
    Well you could always insert the values manually into the DB to at least move forward with your learning. It may also help if you checked the logs to see if the user_id is being saved.

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I forget where it is in the book, but I remember something about entering Stories before the login is added, and then needing to "modify the old"/"delete and add new" Stories after the login has been added. This is something a few have "skipped over" and had trouble with because the book is a bit unclear about it.

  14. #14
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you help a newbie with how to manually insert these values into the DB?

    I'm seriously considering moving on, want to keep going, but not sure how what the impact will be later if I don't get this fixed up...

    Thanks everybody.

  15. #15
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I got a bit carried away in the beginning of the book and created a dozen or so Stories so it was easier to delete them all and enter new ones. But AFAIK, something like this should work
    Code:
    >ruby script/console
    Loading development environment (Rails 2.1.0)
    >> s = Story.find(1)
    => [#<Story id: 1,
     name: "My shiny weblog",
     link: "http://poocs.net/",
     created_at: "2008-08-30 13:31:03",
     updated_at: "2008-08-31 15:46:04",
     user_id: nil>]
    >>s.user_id = 1
    >>s.save
    >>n = Story.find(1)
    >> n = Story.find(1)
    => [#<Story id: 1,
     name: "My shiny weblog",
     link: "http://poocs.net/",
     created_at: "2008-08-30 13:31:03",
     updated_at: "2008-08-31 15:46:04",
     user_id: 1>]
    Change the find() number to get each row and use a good number for the user_id and I think you'll be all set.

  16. #16
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vhenry View Post
    Can you help a newbie with how to manually insert these values into the DB?
    How you do it via the database depends on the database engine you are using.
    Personally, I like using MySQL as you can install the Administrator GUI tools and that makes it easy to monitor and update the datebase directly. You can then run SQL commands directly via the MySQL Query Browser. For example UPDATE.

    However, if you are only just starting to program, you may want to keep to Ruby and Rails at the moment, and move on to SQL hacking when you have those under your belt. In that case, I'd suggest the following:

    Go to the root of your application at the console (cmd in Windows). Then go into Rails console mode from that point. On Windows that will probably look something like this:
    Code:
    c:\rails_root\application\>ruby script/console
    Then do the following:
    Code:
    # find the first user
    user = User.find(:first)
    # then get all the stories
    stories = Story.find(:all)
    # then assign the first user to all the stories
    for story in stories
      story.user = user
      story.save
    end
    exit

  17. #17
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that did the trick. thanks!!!


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
  •