开发者

Undefined method/NoMethodError in Rails 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'


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜