【发布时间】:2015-05-17 13:47:54
【问题描述】:
在模型中我有以下方法,灵感来自RailsCasts #362
模型为 Post,Post 中的一列名为 Author。作者是一个字符串数组。我想将 Author 数组中的每个条目循环到自己的 CSV 文件行中,而不是仅将数组打印到一行。我正在尝试在 Post.rb (模型类文件)中这样做,
但我收到错误“未定义的局部变量或方法randomsched' for #<Class:0xccd0490>"
def self.to_csv(options = {})
CSV.generate(options) do |csv|
author.each do |x|
csv << x
end
end
end
回溯
Started GET "/posts.csv" for 127.0.0.1 at 2015-03-15 07:19:36 -0400
Processing by PostsController#index as CSV
Completed 500 Internal Server Error in 9ms
NameError (undefined local variable or method `author' for #<Class:0xccd0490>):
app/models/post.rb:26:in `block in to_csv'
app/models/post.rb:25:in `to_csv'
app/controllers/posts_controller.rb:10:in `block (2 levels) in index'
app/controllers/posts_controller.rb:8:in `index'
【问题讨论】:
-
需要完整的错误回溯才能调查问题!
-
@maxd 我想我现在已经发布了
-
Post.author 到底是什么?比如
Post.first.author在控制台的输出是什么? -
@thebenedict => [“C”、“B”、“B”、“D”、“C”、“A”、“D”、“B”、“B”、“ D”、“D”、“B”、“A”、“C”、“D”、“D”、“A”、“B”、“C”、“D”、“A”、“C” 、“A”、“A”、“D”、“C”、“B”、“B”、“B”、“C”、“C”、“A”、“B”、“D”、“ C”、“A”、“A”、“B”、“A”、“C”、“D”、“C”、“D”、“D”、“D”、“D”、“C” ,“C”,“A”,“A”,“B”,“B”,“C”,“B”,“A”,“D”,“A”,“C”,“A”,“ B"]
标签: ruby-on-rails ruby arrays csv