SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict DevilBear's Avatar
    Join Date
    Oct 2001
    Location
    Hades
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to troubleshoot RoR

    So I got:

    NoMethodError in GazetteController#show_article

    You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occured while evaluating nil.+

    <sigh>

    The controller action:

    Code:
    def show_article
    
    #test
    @gazette = Gazette.bydate('2006', '06', '04')
    
    render(:layout => 'interior', :template => 'index')
    
    end
    The model method:

    Code:
    def self.bydate(year, month, day)
    
    d = @year + '-' + @month + '-' + @day
    
    n = find_by_sql("select id, published, date_format(published, '%M %D, %Y') as d, headline, body " +
    			"from gazettes where published='#{d}' " +
    			"order by d desc limit 1")
    
    return n[0]
    
    end
    That SQL should return a record with the given test inputs.

    Now I'm not sure how to troubleshoot this... it's not really giving me much to go on.

    Is there any way to get it to output the SQL it actually uses? I'm so used to PHP, building my own queries, and being able to do a quick echo "<p>$query</p>" in the code.

    BTW, any easier way to find by date with RoR?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your first line of defense should be well written unit tests. If you are still having problems the next step is the breakpointer - its not something I have to use often but it can be useful. Simply put the word "breakpoint" anywhere in your code, load your page and then on the command line run script/breakpointer - you'll now be able to inspect whats going on at the point where the breakpoint was encountered.

    More details here:

    http://wiki.rubyonrails.com/rails/pa...WithBreakpoint

  3. #3
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What Luke said, but also, this looks suspect:

    Code:
    def self.bydate(year, month, day)
    d = @year + '-' + @month + '-' + @day
    You send in three params, and then don't use any of them! (You're using instance variables instead of the ones passed into the function.)

    That's the sort of thing that would show up straight away in a unit test.

    Also, why are you not using the date support built into Rails?

    Douglas
    Hello World

  4. #4
    SitePoint Addict DevilBear's Avatar
    Join Date
    Oct 2001
    Location
    Hades
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What date support? You call that date support? ;-)

    RoR seems to spend half its time making my life easier (or promising to) and the other half making it harder... although I suspect that's mostly just learning curve at this point.

    I did notice, sometime after coding the date formatting I need into SQL statements, that it looks like I can access the constants defined in Date via Date::CONSTANTNAME. So at some point I'll go revisit the code, but for now it's getting the job done and I'm on to other parts of the site.

  5. #5
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With your app running, run "ruby script/console" in a terminal window. Also, don't forget about dev log files (very helpful for debugging): app/logs/ ...or something like that.

  6. #6
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just another note that many people often forget, is to use Breakpoint.breakpoint in your scripts, I find it can be a better than script/consolse as script/console only seems to be useful for interactive testing models normally.

    Both when using script/console and the breakpointer, look at Dispatcher.application_reset, it will allow you to see changes in you models after updating the source
    http://virtualfunction.net - Rails Web Development
    http://squaremove.co.uk - Rails powered Property Listings


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
  •