0

我在导入 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">&times;</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'”

4

2 回答 2

0

我猜您还没有添加routes该方法import,请在您的添加以下内容routes.rb

resources :employee_attendances  do 
  post 'import' 
end

重新启动您的服务器,然后按照以下行

<%= form_tag import_employee_attendance_path, multipart: true do %>
于 2013-08-07T09:37:27.603 回答
0

将此添加到您的路线文件中:

resources :employee_attendances do
  collection do
    post 'import'
  end
end

或这个 :

resources :employee_attendances do
  post 'import', on: :collection
end

这个要查看:

<%= form_tag import_employee_attendances_path, multipart: true do %>

谢谢

于 2013-08-07T09:55:59.447 回答