我在導入csv文件時遇到了問題。我的錯誤是「Employee_attendances#index中的NameError」。如何在導入csv時解決NameError問題
模型
class EmployeeAttendance < ActiveRecord::Base
attr_accessible :date, :emp_id, :in_time, :out_time, :status
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
@employee_attendance = EmployeeAttendance.find_by_emp_id_and_date(row['employee_id'],row['date'].to_date.strftime("%Y-%m-%d")) || EmployeeAttendance.new
@employee_attendance.emp_id = row['emp_id']
@employee_attendance.in_time = row['in_time']
@employee_attendance.out_time = row['out_time']
@employee_attendance.status = row['status']
@employee_attendance.date = row['date']
@employee_attendance.save!
end
end
end
在控制器
class EmployeeAttendancesController < ApplicationController
def index
end
def new
end
def create
end
def import
EmployeeAttendance.import(params[:file])
redirect_to EmployeeAttendance_path, notice: "Sucessfully Created."
end
end
鑑於(index.html.erb)
<% if flash[:notice].present? %>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= flash[:notice] %>
</div>
<% end %>
<div>
<h2>Employee Attendance</h2>
</div>
<%= form_tag import_employee_attendance_index_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import", :class => 'btn btn-primary' %>
<% end %>
它表示如「未定義的局部變量或方法`import_employee_attendance_index_path」錯誤#<#:0xb30a8c88>「
看來,你沒有路由定義,或者你拼錯了。請檢查'rake routes'的結果。 – vee
請提供'routes.rb'代碼 – Salil
你能分享你的路線嗎? –