在每个现有属性上获取 NoMethodError(未定义的方法)
Getting NoMethodError (undefined method) on every EXISTING properties
如果我在控制台中输入 movie = Movie.limit(1)
,我会得到
SELECT "movies".* FROM "movies" LIMIT [["LIMIT", 1]] => #<ActiveRecord::Relation [#<Movie id: 2, title: "Superman", rating: "PG", total_gross: 0.134218018e9, created_at: "2019-02-20 07:42:18", updated_at: "2019-02-20 08:17:24", description: "Clark Kent grows up to be the greatest super-hero", released_on: "1978-12-15">]>
但是,如果我输入 movie.title
,而不是 "Superman",我会得到:
Traceback (most recent call last):
1: from (irb):2
Movie Load (0.6ms) SELECT "movies".* FROM "movies" LIMIT [["LIMIT", 1]]
NoMethodError (undefined method `title' for #<Movie::ActiveRecord_Relation:0x000055b334842668>)
花了最后三个小时寻找原因无济于事。
编辑:我的 class 有方法:
def index
@movies = Movie.all
end
但是当我从索引视图调用它时,none 的属性有效:
<% @movies.each do |f| %>
f.title
<% end %>
电影中的参数错误#index
更新: 我的数据库中有一个空白行,每个 属性 返回 nil。删除此行解决了问题。
如果我在控制台中输入 movie = Movie.limit(1)
,我会得到
SELECT "movies".* FROM "movies" LIMIT [["LIMIT", 1]] => #<ActiveRecord::Relation [#<Movie id: 2, title: "Superman", rating: "PG", total_gross: 0.134218018e9, created_at: "2019-02-20 07:42:18", updated_at: "2019-02-20 08:17:24", description: "Clark Kent grows up to be the greatest super-hero", released_on: "1978-12-15">]>
但是,如果我输入 movie.title
,而不是 "Superman",我会得到:
Traceback (most recent call last):
1: from (irb):2
Movie Load (0.6ms) SELECT "movies".* FROM "movies" LIMIT [["LIMIT", 1]]
NoMethodError (undefined method `title' for #<Movie::ActiveRecord_Relation:0x000055b334842668>)
花了最后三个小时寻找原因无济于事。
编辑:我的 class 有方法:
def index
@movies = Movie.all
end
但是当我从索引视图调用它时,none 的属性有效:
<% @movies.each do |f| %>
f.title
<% end %>
电影中的参数错误#index
更新: 我的数据库中有一个空白行,每个 属性 返回 nil。删除此行解决了问题。