我在导入 csv 文件时遇到问题。我收到类似“Employee_attendances#index 中的 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 %>
它显示错误,例如“#<#:0xb30a8c88> 的未定义局部变量或方法 `import_employee_attendance_index_path'”