使用集合选择在rails应用程序中导入CSV数据

时间:2018-02-22 14:49:09

标签: ruby-on-rails-5 csv-import

我正在尝试允许导入器在导入期间选择集合选择选项。现在导入器工作,但是在导入过程中它没有选择模型类型。

我的架构:

  create_table "hardwares", force: :cascade do |t|
    t.string "serialnumber"
    t.string "modelnumber"
    t.string "location"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "poc_id"
    t.integer "modeltype_id"
    t.index ["modeltype_id"], name: "index_hardwares_on_modeltype_id"
    t.index ["poc_id"], name: "index_hardwares_on_poc_id"
  end

  create_table "modeltypes", force: :cascade do |t|
    t.string "mtype"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "pocs", force: :cascade do |t|
    t.string "name"
    t.string "address"
    t.string "facility"
    t.string "phone"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

hardware.rb

class Hardware < ApplicationRecord
  validates_uniqueness_of :serialnumber
  def self.import(file)
    if file == nil
    else
      CSV.foreach(file.path, headers: true) do |row|
        Hardware.create row.to_hash
      end
    end
  end
end

在我的硬件控制器中导入Def:

def import
    Hardware.import(params[:file])
    redirect_to hardwares_url, notice: 'Hardware was successfully imported.'
end

CSV文件

serialnumber,modelnumber,location,poc_id,modeltype_id
S123,M1,deployed,Joe,Dell
S456,M2,local,Shmoe,HP

当我运行导入器时,它会填充序列号和ModelNumber并忽略location,poc_id和modeltype_id。如何在导入期间使用下拉菜单。基本上我希望它在下拉列表中搜索匹配并选择它导入。

0 个答案:

没有答案