打开 index 的视图页面,出现以下错误提示:
SQLite3::SQLException: no such column: name: SELECT "nius".* FROM "nius" ORDER BY name
代码的错误信息如下:
Extracted source (around line #20):
17: <th>第9-10节</th>
18: <th>第11-12节</th>
19: </tr>
20: <% @nius.each do |niu| %>
21: <%= puts niu.class %>
22: <tr>
23: <td><%= niu.id %></td>
niu 控制器代码如下:
class NiusController < ApplicationController
def index
@nius = Niu.order(:name)
respond_to do |format|
format.html
format.csv { send_data @nius.to_csv }
#format.xlsx { send_data @nius.to_csv(col_sep: "\t") }
end
end
def import
Niu.import(params[:file])
redirect_to root_url, notice: "Nius imported."
end
end
niu 模型代码如下:
class Niu < ActiveRecord::Base
attr_accessible :room, :c1toc2,:c3toc4,:c5toc6,:c7toc8,:c9toc10,:c11toc12
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |niu|
csv << Niu.attributes.values_at(*column_names)
end
end
end
def self.import(file)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
niu = find_by_id(row["id"]) || new
niu.attributes = row.to_hash.slice(*accessible_attributes)
niu.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path)
when ".xls" then Excel.new(file.path,)
when ".xlsx" then Roo::Excelx.new(file.path, file_warning: :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
end
数据库迁移文件如下:
class CreateNius < ActiveRecord::Migration
def change
create_table :nius do |t|
t.string :room
t.string :c1toc2
t.string :c3toc4
t.string :c5toc6
t.string :c7toc8
t.string :c9toc10
t.string :c11toc12
t.timestamps
end
end
end
请教前辈们,到底是哪里错了呢?