SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question a problem with the v.story in chapter 7

    Hi. I have been playing with PHP and JavaScript for some time now and wanted to try ROR. So far the whole thing has been a breeze. I then hit a small glitch which I need to eradicate before continuing...

    I bought Patrick Lenz's book and have followed the instructions rigorously (spotted an error in printed RHTML) and have reached chapter 7 - Ajax and Web 2.0 (p-192), whereupon I have discovered that the v.story call doesn't work; it simply sends back an error:
    Code:
    >> v.story
    NoMethodError: undefined method `story' for #<Vote:0x247cc40>
            from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
            from (irb):33
    >>
    I have checked and rechecked the files with the book and can find no discrepancies

    I am running OSX 10.4.10

    If any more information is needed, I am ready to supply all.
    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you post your vote and story models?
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  3. #3
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    firstly, my vote model (as I understand it. I am still new to ROR)
    vote.rb
    Code:
    class Vote < ActiveRecord::Base 
    belongs_to :story
    end
    story.rb
    Code:
    class Story < ActiveRecord::Base
    validates_presence_of :name, :link
    has_many :votes
    end
    create_votes.rb
    Code:
    class CreateVotes < ActiveRecord::Migration
    def self.up
    create_table :votes do |t|
    t.column :story_id, :integer
    t.column :created_at, :datetime
    end
    end
    def self_down
    drop_table :votes
    end
    end
    If you need any more in case I have left anything out, please shout.

  4. #4
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    i also have reached here and have the same error, my files are the same as above. would be interested to see the resolution.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you access s.votes ? Or is it only v.story that is failing?
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  6. #6
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes s.votes works fine
    Code:
    >> s.votes
    => [#<Vote:0x47d55ec @new_record=false, @errors=#<ActiveRecord::Errors:0x47d2bbc @errors={}, @base=#<Vote:0x47d55ec ...>>, @attributes={"id"=>1, "story_id"=>2, "created_at"=>Wed Sep 19 12:05:20 +0100 2007}>]
    >> v.story
    NoMethodError: undefined method `story' for #<Vote:0x47bd4d8>
            from D:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
            from (irb):20

  7. #7
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have decided to have another go to see if I can replicate the error. I have found that migrating is awkward when you start again from scratch and no databases. But I have persevered and will try again this weekend. I'll get v.story to behave.

  8. #8
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    thanks much appreciated.
    its frustrating when you can only go so far and because i am a total noob to this i wouldn't know where to start looking for errors

  9. #9
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. That was sorted out. I started from scratch again and was almost successful. v.story isn't a problem any more. However, I cannot preview either index.rhtml (without a refresh) and show.rhtml is broken (not visibly, and I even retraced steps to see whether I missed anything). The output from the browser says:

    Code:
    NoMethodError in Story#show
    Showing app/views/story/show.rhtml where line #3 raised
      You have a nil object where you didn't expect it!
      The error occured while evaluating nil.votes_count
    Extracted source (around line #3):
        1.  <h2>
        2.    <span id="vote_score">
        3.    Score: <%= @story.votes_count %>
        4.    </span>
        5.  </h2>
    Not surprisingly, it fails a bunch of tests saying is expects 'x' and observed 'y'. I have absolutely no idea why. I have spent two days going over the whole thing and have drawn blanks everywhere. The files I used are identical to the downloadable versions from the rails book section of the sitepoint site.

    I feel that not being able top log out of the shovell app is a symptom of some deep rooted malevolence within the code I have created, even when I have compared it to the code downloads, found it to be exact and have checked the database thoroughly. I must be missing something really simple here.

    For the most part, RoR is incredibly straightforward, but when you get a curve ball as a noob, it can be as daunting as learning C# if you're a Masai warrior! I don't know C#.

  10. #10
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you think this line means?:
    "The error occured while evaluating nil.votes_count"
    Look the code in the error and identify what that could possibly be referring to.

    Once you figure out what that means, trace your way back to figure out how that might happen.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  11. #11
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a thought,

    I had an nil.votes_count issue

    I had my show and vote definitions listed under the protected fetch_stories definition of my StoryController class definition.

    I moved these methods above the protected section and it solved my nil response for my @story object

    hope this helps

    goblin

  12. #12
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks samsm, the line in the file gets the score of votes for the class '@story' and displays them in a page with a path called 'permalink' i.e. 0.0.0:3000/story/show/permalink. I have traced back the votes class, story class and permalink class and can see nothing in error having compared them line for line with the downloaded source files.

    I have studied the story_controller file from top to bottom and can not determine anything wrong, even though the test results come back with failures for deprecation, they disappear if I change another aspect of the application.

    I am going to go back to the end of chapter 6 and work from that point again.

    I do not have a protected fetch_stories part in my story_controller yet, so that isn't the answer. Thanks anyway.

  13. #13
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I decided to hold off doing the whole thing again and tried a few things that I knew were going to fail. However, the results were not as expected.

    It seems that the 'find_by :permalink' query is in fact looking for the name instead. I changed the permalinks throughout to camel case and did a little browsing. The ONLY permalink that works is a single word permalink and name, so changing them all to camel case seemed to be an option. However, it is still loading up my+shiny+weblog (or failing in its attempt to) instead of myShinyWeblog.

    That said and posted, I still cannot fathom what the problem is as my experience in coding has been more of a grab, study, edit approach as opposed to a create from scratch one (with the exception of html and css).

  14. #14
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Now I'm even more confused!!!

    Checking and re-checking is all very well and good but I have discovered a small discrepancy which could be the answer to the issue. The dynamic link in the index.rhtml that randomly changes depending on refresh seems to have a tendency to put a '+' symbol between the words in the permalinks instead of a '-'. It may do that anyway, but it follows that when looking for 'sample-permalink' it is finding 'sample+permalink' and therefor cannot complete the function.

    Now it gets really confusing (for me at least). If I change the '+' to a '-' in the address bar, it returns the same error, which is incidentally the self same error as I listed previously.

    This may seem like a really stupid question but, do I have to populate all of the permalink fields in the database? There is only one populated at the moment and when I change the '+' to '-' on that one it works a treat.

    I am going for a long walk in the woods to look at something else for a change.

  15. #15
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what's the specific problem now? How do you want the application to work? How is it working now? What specifically needs to change?

    Does find_by :permalink need to work differently?
    Does the state of your development database need to change? (if so, just scrap it, start over)

    Isolate your problem. If you take three steps back, any problem in any application is incredibly complicated. get really close to a problem, and the cause is likely simple.

    For example, in post #9, the problem was that @story was nil. Once you learn (through the view) that @story isn't being properly loaded with a Story, go back to the place where that should be happening until you can identify specifically why.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  16. #16
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks samsm. Sage words you write. I think that as I am new to a thing, I am scared to look at the obvious and disregard it as irrelevant when sometimes, that is the correct approach.

    The problem was not with the @story. I figured that out as it broke before the @story class was called. It was trying to get information for :vote and I had somehow loused up something in that part. That was what was throwing out the errors and stuff. I have indeed scrapped it and started over.

    I think that until I fully grasp all of the tenets thus far, I should get to a certain point in the book + 1 and then start again from scratch. That way, I should be able to accomplish the preliminary stages with my eyes shut within two weeks.

    Being half way through the book is fine but it is nowhere near half way through the process and I have so much more that I need to learn before I begin playing with new, more complicated models. some of which have yet to be done online. I have aspirations and the keenest students sometimes ask the dumbest questions, even if they know the answer; they just need a nudge to let them know that they already know it.


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
  •