views:

76

answers:

3

So I am doing a lynda.com Rails essential training tutorial, and I am getting an error that the video tutorial does not. I assume this has to do with the different versions of Ruby & Rails that I am using (the latest as of today) as opposed to the ones they used when this was recorded (I think in 2007).

This is what my controller looks like:

class PublicController < ApplicationController

def album_list
    @albums = Album.find_by_sql('SELECT * FROM albums;')
end 

def alt_album_list
    release_date = '2011-10-01'
    artist = 'Paul'
    @albums = Album.find(:all, 
        :conditions => ["release_date <= ? AND artist LIKE ?", release_date, '%' + artist + '%'],
        :order => 'release_date ASC',
        :limit => 1, :offset => 1)
    render(:action => 'album_list')
end

def one_album_list
    release_date = '2011-10-01'
    artist = 'Paul'
    @album = Album.find(:first, 
        :conditions => ["release_date <= ? AND artist LIKE ?", release_date, '%' + artist + '%'],
        :order => 'release_date ASC')               
end

end

This is what my view looks like:

<html>
<head>
<title></title>
</head>

<body>

    Title: <%= @album.title %><br />
    Artist: <%= @album.artist %><br />
    Genre: <%= @album.genre %><br />


</body>
</html>

The error I get when I load it is:

NoMethodError in Public#show_album

Showing C:/Users/<username>/music_library/app/views/public/show_album.rhtml where line #8 raised:

undefined method `title' for nil:NilClass
Extracted source (around line #8):

5: 
6: <body>
7: 
8:  Title: <%= @album.title %><br />
9:  Artist: <%= @album.artist %><br />
10:     Genre: <%= @album.genre %><br />
11:     
Rails.root: C:/Users/<username>/music_library

Application Trace | Framework Trace | Full Trace
app/views/public/show_album.rhtml:8:in `_app_views_public_show_album_rhtml___574395264_32579964__949661750'
+2  A: 

Let me start by telling you this is horrible outdated if the code sample

def album_list
    @albums = Album.find_by_sql('SELECT * FROM albums;')
end

Is really in that video stop watching now since it will do more good then bad.

Ruby on Rails has some very up-to-date and well written documentation at guides.rubyonrails.com and I've heard great things about http://railstutorial.org/ as well.

Now I will try to answer your question as well but since that code makes almost no sense it's pretty hard.

Change

def album_list
    @albums = Album.find_by_sql('SELECT * FROM albums;')
end  

to

def index
    @albums = Album.all
end 

Move & rename your view to app/views/albums/index.html.erb

And make sure you change

  Title: <%= @album.title %><br />
  Artist: <%= @album.artist %><br />
  Genre: <%= @album.genre %><br />

to

   <% @albums.each do |album| %>
    Title: <%= album.title %><br />
    Artist: <%= album.artist %><br />
    Genre: <%= album.genre %><br />
  <% end %>

and define a route in your routes file resources :albums

I hope this helps you on the right path to mastering Rails.

Maran
Well the tutorial was just showing us the first line (from an illustration purposes to show us how we COULD do it using straight SQL). But if you look further down, you will see the rails way (of using rails methods) of accessing the db - rather than using straight SQL syntax like that. You are right though, but they pointed that out.
marcamillion
Definitely check out railsguides, as well as the screencasts on the official Rails website.
Jacob
A: 

undefined method `title' for nil:NilClass

This error shows that no records were found in your database and @album was set to nil. Be sure to provide some guarding code before attempting to access properties of an object fetched from the database.

Tumas
So this error is showing that there are nothing was returned from the db? Is that how I read an error like this in the future? I thought it was saying something like there is no title characteristic of that method - so that's why it was confusing me. If it is a no entry found in the db, that makes total sense and I know how to correct that. But I was struggling with the object/method issue. Thanks.
marcamillion
This kind of error shows that @album wasn't assigned any particular value in controller. You could have forgotten to assign anything to it or yes, it could have been the result of record that wasn't found in the database. One additional note: IMHO, it's best to concentrate code that access the database in Models. Controllers should be responsible for other things, mainly setting variables from model methods for views, handling errors returned from models (like empty album), choosing which view to load, etc.
Tumas
A: 

I figured it out. I was selecting the wrong action 'show_action' when the action in my controller was 'one_album_list'.

So when I renamed the action to 'show_action' and went to url/public/show_action it works.

marcamillion