新手问题 SQLite3::SQLException: no such column: name:错误

fengfans · 2015年12月06日 · 最后由 fengfans 回复于 2015年12月07日 · 4117 次阅读

打开 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

请教前辈们,到底是哪里错了呢?

低级错误

@nius = Niu.order(:name)

新表中已经没有这个字段,怎么会不报错。浪费大家的时间了。

控制器第一行 @nius = Niu.order(:name)

要按 name 排序,但是模型里没有 name 这个属性

#1 楼 @fengfans 别这么说,大家都有这阶段

t.string :c1toc2
t.string :c3toc4
t.string :c5toc6
t.string :c7toc8
t.string :c9toc10
t.string :c11toc12  

怎么这么怪,是有啥特殊意义吗?

第 1-2 节,第 3-4 节,依此类推。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号