2013-08-07 53 views
0

我在導入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">&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 %> 

它表示如「未定義的局部變量或方法`import_employee_attendance_index_path」錯誤#<#:0xb30a8c88>「

+0

看來,你沒有路由定義,或者你拼錯了。請檢查'rake routes'的結果。 – vee

+0

請提供'routes.rb'代碼 – Salil

+0

你能分享你的路線嗎? –

回答

0

添加到您的路徑文件:

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

or this:

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

和這觀點:

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

感謝

+0

非常感謝它對我有用。 – user2310209

0

我猜你沒有爲方法import添加routes,添加以下你routes.rb

resources :employee_attendances do 
    post 'import' 
end 

重新啓動服務器,然後下面一行

<%= form_tag import_employee_attendance_path, multipart: true do %> 
+0

我得到同樣的錯誤。請幫我 – user2310209

+0

請提供'routes.rb'代碼,或者o/p的'rake routes | grep'import'' – Salil

+0

我已經給routes.rb文件中的employee_attendances – user2310209