Ruby on rails Rails 5-如何将数据从xls文件导入数据库
我正在尝试找出如何将xls文件中的数据添加到rails 5 psql数据库中 我曾试图学习GoRails教程,但这些教程很快就变得无关紧要,因为它们使用应用程序中某个地方创建的CSV文件来完成教程。我不知道如何将我的xls数据转换为csv数据,以便与教程一起使用 我也尝试过这些教程中的每一个。我不能让他们中的任何人工作 我试过了,而且,还有。我不能让他们中的任何人工作 对于我在博客上发布的第一篇教程中的一个问题,我的回答是:采取文章中没有提到的步骤。我一点也不清楚我是否应该填补分步说明中的空白 我概述了我在这方面的努力。我无法找到帮助来让xls上传工作。因此,我再次提出帮助请求——鉴于第一篇So帖子变得很长,我进行了各种尝试来实施建议的解决方案 我有:Ruby on rails Rails 5-如何将数据从xls文件导入数据库,ruby-on-rails,ruby,excel,csv,import-from-excel,Ruby On Rails,Ruby,Excel,Csv,Import From Excel,我正在尝试找出如何将xls文件中的数据添加到rails 5 psql数据库中 我曾试图学习GoRails教程,但这些教程很快就变得无关紧要,因为它们使用应用程序中某个地方创建的CSV文件来完成教程。我不知道如何将我的xls数据转换为csv数据,以便与教程一起使用 我也尝试过这些教程中的每一个。我不能让他们中的任何人工作 我试过了,而且,还有。我不能让他们中的任何人工作 对于我在博客上发布的第一篇教程中的一个问题,我的回答是:采取文章中没有提到的步骤。我一点也不清楚我是否应该填补分步说明中的空白
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)
控制器:
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
我的表格有:
<%#= 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 %>
我的创建操作的第13行说:
Randd::Field.create(params[:randd_field][:file])
下一个预感:我的资源是嵌套的。Randd是最高级别,field是孩子
该模型称为:
class Randd::Field < ApplicationRecord
class Randd::FieldsController < ApplicationController
表单使用此开头行:
<%= simple_form_for @field, multipart: true do |f| %>
我想知道这个问题是否与在这些地方使用randd_字段和表单中的@field有关?如果我尝试将其更改为@randd_字段,并且我不知道在哪里可以找到ruby或rails关于如何在任何完整、准确的表单中使用名称空间的规则,那么该表单将无法工作
有人能帮忙看看这里出了什么问题吗?或者,是否有人引用了一个完整的教程,该教程不依赖于我猜测缺少的步骤是什么,或者如何将其转换为xls而不是.csv文件?有一些ruby xls解析器可以使用。Creek、Simple Spreadsheet和Roo。有一些ruby xls解析器可以使用。Creek、简单电子表格和Roo
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])