Rails 5如何将数据从xls文件导入数据库

我想弄清楚如何从xls文件添加数据到我的rails 5 psql数据库。

我试图按照GoRails教程 – 但这些教程很快就变得无关紧要,因为它们使用在应用程序中创build的CSV文件来完成教程。 我不知道如何将我的xls数据转换为csv数据以跟随该教程。

我也尝试了这些教程。 我无法让他们中的任何一个工作。

我已经试过这个教程 , 这一个 , 这一个 。 我无法让他们中的任何一个工作。

对我在第一篇教程的博客上发布的问题的回应说,要采取一个在post中没有列出的步骤。 我一点也不明白,我应该一步一步地填补空白。

我概述了我在这个SO职位上的工作 。 我无法find帮助,让xls上传工作。 所以,我再次提出了一个求助的要求 – 因为第一个SOpost变得很长,我尝试了所有的尝试来实现build议的解决scheme。

我有:

class Randd::Field < ApplicationRecord require 'csv' # def self.import(file) # CSV.foreach(file.path, headers: true) do | row | # Randd::Field.create! row.to_hash # end # end def self.create(file) CSV.foreach(file.path, headers: true) do |row| randd_field_hash = row.to_hash # exclude the price field randd_field = Randd::Field.where(id: randd_field_hash["id"]) if randd_field.count == 1 randd_field.first.update_attributes(randd_field_hash) else Randd::Field.create! row.to_hash#(randd_field_hash) end # end if !product.nil? end # end CSV.foreach end # end self.import(file) 

第一个预感 :在这个模型中,我需要'csv'。 我必须要求“xls”或其他的东西,因为我实际上并没有一个逗号分隔值列表来导入任何地方?

的routes.rb

 namespace :randd do resources :fields do collection do post :create end end end 

控制器:

 class Randd::FieldsController < ApplicationController def index @randd_fields = Randd::Field.all end def new @field = Randd::Field.new end def create # @field = Randd::Field.new(randd_field_params) Randd::Field.create(params[:randd_field][:file]) redirect_to action: "index", notice: "Data imported" end def show redirect_to action: "index" end 

索引视图:

  <table class="table table-bordered"> <tr> <td> <h5>Title</h5> </td> <td> <h5>Reference (ANZ)</h5> </td> <td> <h5>Added</h5> </td> <%# if policy(@package_ips).update? %> <td> <h5>Manage this asset</h5></td> <%# end %> </tr> <% @randd_fields.each do | field | %> <td> <%= field.title %> </td> <td> <%= field.anz_reference %> </td> <td> <%= field.created_at.try(:strftime, '%e %B %Y') %> </td> <% end %> </tr> </table> 

上面显示的注释组件显示了我试图用来使这个工作的所有各种选项。

 # def import # # byebug # # Randd::Field.import(params[:file]) # # Randd::Field.create(params[:randd_field]['file']) # # redirect_to action: "index", notice: "Data imported" # end private def randd_field_params params.fetch(:randd_field, {}).permit(:title, :anz_reference) end end 

我的表单有:

 <%#= simple_form_for (@field), multipart: true do |f| %> <%= simple_form_for @field, multipart: true do |f| %> <%= f.error_notification %> <div class="form-inputs" style="margin-bottom: 50px"> <div class="row"> <div class="col-md-12"> <div class="form_title">Research Field Codes</div> </div> </div> <div class="row"> <div class="col-md-12"> <%= f.file_field :file %> </div> </div> </div> <div class="row"> <div class="col-md-10 col-md-offset-1" style="margin-top: 50px"> <div class="form-actions"> <%= f.button :submit %> </div> </div> </div> <% end %> 

出现这种尝试的错误消息说:

 NoMethodError - undefined method `[]' for nil:NilClass: app/controllers/randd/fields_controller.rb:13:in `create' 

我的创build行动13行说:

 Randd::Field.create(params[:randd_field][:file]) 

下一个预感:我的资源是嵌套的。 兰德是最高级别的领域是孩子。

该模型被称为:

 class Randd::Field < ApplicationRecord 

控制器被称为:

 class Randd::FieldsController < ApplicationController 

控制器中允许的参数定义如下:

  params.fetch(:randd_field, {}).permit(:title, :anz_reference) 

表格使用这个开头:

 <%= simple_form_for @field, multipart: true do |f| %> 

在错误消息中突出显示的行有:

 Randd::Field.create(params[:randd_field][:file]) 

我不知道这个问题是否与在这些地方使用randd_field有关,只是在@field的forms? 如果我尝试将其更改为@randd_field,并且我不知道如何在任何完整,准确的表单中查找如何使用命名空间的ruby或rails规则,则表单不起作用。

任何人都可以帮忙看看这里有什么问题吗? 或者,任何人都有一个完整的教程,不依赖于我猜测缺less的步骤是什么或如何将其转换为使用xls而不是.csv文件?

有几个ruby xlsparsing器可以使用。 溪,简单的电子表格和Roo。