SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Rails 2.0 problem Chapter 5

    Hi

    I have a littel problem and was wondering if anyone could help. I hav been doing the Simply Rails 2.0 PDf and have hit a problem right on the end of chapter 5. Have been using Netbeans and MySql database for the project.

    Code for controller index.html.eb is -

    A random link:
    <&#37;= link_to @story.name, @story.link %>

    Code for stories controller is -

    def index
    @story = Story.find_by_name('SitePoint Forums')
    end

    Pulling the data causes thos error -

    NoMethodError in Stories#index

    Showing stories/index.html.erb where line # raised:

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

    Extracted source (around line #):

    RAILS_ROOT: C:/Documents and Settings/Norm/My Documents/NetBeansProjects/Shovell
    Application Trace | Framework Trace | Full Trace

    app/views/stories/index.html.erb:3:in `_run_erb_47app47views47stories47index46html46erb'
    :1:in `start'

    Any help will be grateful..there are story objects in the database as have proved it through ruby console....
    Last edited by Normboy; Jun 26, 2008 at 15:36. Reason: mong! Sorted!

  2. #2
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorted :mong

  3. #3
    SitePoint Zealot daveporter's Avatar
    Join Date
    Mar 2002
    Location
    Perth, Western Australia
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing that the name did not match exactly ?

    If the name you entered was slightly different then you will get a nil object error.

    It happened to me and took me a while to figure it out!

    cheers, Dave

  4. #4
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep fell for taht as well!

    cheer for the reply.....

    Though hav enot be able to implement the StoriesController to dispaly @story variable as Random..get this error...

    Mysql::Error: #42000FUNCTION shovell_development.RANDOM does not exist: SELECT * FROM `stories` ORDER BY RANDOM() LIMIT 1

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi mate,

    the problem is that the writer is using SQLite and you are using MySQL. You just need to change it to this:

    Code:
    @story = Story.find(:first, :order => 'RAND()')
    Hope that helps.

  6. #6
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks it worked.. need to do a bit of studying on MySQL me thinks!

    This forumn is graet..quick replys!

    Many thanks!!

    NormBoy


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
  •