How to solve NameError while importing csv in rails -
i having problem in importing csv files.i got error "nameerror in employee_attendances#index".
model
class employeeattendance < activerecord::base attr_accessible :date, :emp_id, :in_time, :out_time, :status def self.import(file) csv.foreach(file.path, headers: true) |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
in controller
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
in view (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 %> <%= file_field_tag :file %> <%= submit_tag "import", :class => 'btn btn-primary' %> <% end %>
it showing error "undefined local variable or method `import_employee_attendance_index_path' #<#:0xb30a8c88>"
add routes file :
resources :employee_attendances collection post 'import' end end
or :
resources :employee_attendances post 'import', on: :collection end
and view:
<%= form_tag import_employee_attendances_path, multipart: true %>
thanks
Comments
Post a Comment