我试图找出如何将数据从xls文件添加到我的rails 5 psql数据库。
我曾尝试按照GoRails教程进行操作 - 但这些教程很快就变得无关紧要了,因为他们使用在应用程序中创建的CSV文件来完成教程。我不知道如何将我的xls数据转换为csv数据以跟随该教程。
我也尝试过这些教程。我无法让他们中的任何一个工作。
我尝试了this tutorial,this one和this one。我无法让他们中的任何一个工作。
对我在第一个教程的博客上发布的问题的回答说,采取了一篇未在帖子中概述的步骤。对我来说一点也不明显,我应该填写分步说明中的空白。
我概述了尝试让这个工作在SO post中。我无法找到帮助以使xls上传工作。所以,我再次提出求助请求 - 鉴于第一篇SO帖子变得很长,我做了各种尝试来实现建议的解决方案。
我有:
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)
第一次预感:在这个模型中,我需要&#39; csv&#39;。我是否需要要求xls&#39;或者其他什么,因为我实际上没有可以导入的逗号分隔值列表?
的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'
我的创建操作的第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有关?如果我尝试将其更改为@randd_field并且我不知道在何处找到关于如何以任何完整,准确的形式使用命名空间的ruby或rails规则,该表单不起作用。
任何人都可以帮忙看看这里有什么问题吗?或者,是否有人引用了完整教程,该教程并不依赖于我猜测丢失的步骤是什么或如何将其转换为与xls而不是.csv文件一起使用?
答案 0 :(得分:0)
你可以使用一些ruby xls解析器。 Creek,Simple Spreadsheet和Roo。