Ruby on rails 396导入CSV和Excel找不到没有ID的员工
我正在使用rails 4。我遵循Railscast教程并选择了该文件,但在导入时,我在/employees/import处发现了以下错误:ActiveRecord::RecordNotFound 找不到没有ID的员工 宝石路 路由文件Ruby on rails 396导入CSV和Excel找不到没有ID的员工,ruby-on-rails,ruby,excel,csv,Ruby On Rails,Ruby,Excel,Csv,我正在使用rails 4。我遵循Railscast教程并选择了该文件,但在导入时,我在/employees/import处发现了以下错误:ActiveRecord::RecordNotFound 找不到没有ID的员工 宝石路 路由文件 resources :employees do collection { post :import } end 模型 员工控制员 class EmployeesController < ApplicationController
resources :employees do
collection { post :import }
end
模型
员工控制员
class EmployeesController < ApplicationController
before_action :set_employee, only: [:show, :edit, :update, :destroy, :import]
# GET /employees
# GET /employees.json
def index
@employee = Employee.where(:status => true)
@turnover_employee = Employee.where(:status => false)
respond_to do |format|
format.html
format.csv { send_data @employee.to_csv}
format.xls #{ send_data @employee.to_csv(col_sep: "\t") } /// Alternativa ///
end
end
def import
Product.import(params[:file,:id])
redirect_to root_url, notice: "Products imported."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
@employee = Employee.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:identifier, :first_name, :last_name, :address, :phone, :status, :service_company_id)
end
end
进口索引表
<%= form_tag import_employees_path, multipart: true do %>
<%= file_field_tag :file, :class=> "btn icon-upload" %> |
<%= submit_tag "Importar", :class=> "btn icon-upload" %>
<% end %>
在模型的导入类方法中类似:
employee = Employee.where(id: employee_hash["id"]) || new
在employee_哈希[id]中找不到值。因此,您实际上是在做Employee.whereid:nil
我看不到employee\u hash设置到任何位置。我使用以下代码使其工作:
def self.import(file)
allowed_attributes = ["identifier","first_name","last_name","address","phone"]
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
employee = find_by_identifier(row["identifier"]) || new
employee.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
employee.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
我更改id以搜索标识符。因为在excel中
员工没有id,但有标识符。并删除
没有帮助。
另一个变化是'
发件人:
.csv然后csv.newfile.path,nil,:忽略
致:
删除rails 4所需的“iconv”工程
让它为csv工作,我会发布一个代码更新,看看我是否能在代码上得到一些帮助。谢谢你的回复,如果你能解释一下你做了什么,而不仅仅是发布代码,fastIt会让你的回答更好。
def self.import(file)
allowed_attributes = ["identifier","first_name","last_name","address","phone"]
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
employee = find_by_identifier(row["identifier"]) || new
employee.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
employee.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::Csv.new(file.path, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
".csv" then Roo::Csv.new(file.path, :ignore)