简体   繁体   English

Rails 3中未定义的方法/ NoMethodError

[英]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. 因此,我正在做lynda.com Rails基本培训教程,但我收到了视频教程没有的错误。 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). 我认为这与我使用的Ruby&Rails的不同版本(今天的最新版本)有关,而不是与记录时使用的版本(我认为是2007年)有关。

This is what my controller looks like: 这是我的控制器的外观:

class PublicController < ApplicationController 类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. Ruby on Rails在guides.rubyonrails.com上有一些非常最新且写得很好的文档,我也听说过有关http://railstutorial.org/的很棒的事情。

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 将视图移动并重命名为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 并在您的路线文件resources :albums定义一条路线resources :albums

I hope this helps you on the right path to mastering Rails. 我希望这可以帮助您正确掌握Rails。

undefined method `title' for nil:NilClass nil:NilClass的未定义方法“标题”

This error shows that no records were found in your database and @album was set to nil . 此错误表明在数据库中未找到任何记录,并且@album设置为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'. 当控制器中的动作为“ one_album_list”时,我选择了错误的动作“ show_action”。

So when I renamed the action to 'show_action' and went to url/public/show_action it works. 因此,当我将操作重命名为“ show_action”并转到url / public / show_action时,它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM