How can show message error in a view, when upload a excel file?

1.4k Views Asked by At

I'm trying to show a message on the new view

Here the live demo:

http://code.runnable.com/VrUvfZPOiiVooTAU/importing-excel-files-for-ruby-on-rails

Here is the controller:

def new
end

def create
   @errors = User.import(params[:file])          
   if @errors.present? 
     render :new;
     return; 
   end
end

Here is the view

 Import
 <%= form_tag user_management_users_path, multipart: true do %>
  <%= file_field_tag :file  %>
  <%= submit_tag "Import" %>
 <% end %>


 <% if @errors %>
   <% @errors.each do |message| %>
     <%= message %> 
   <% end %>
 <% end %>

Here is the model:

def self.import(file)
 @errors = []
 spreadsheet = open_spreadsheet(file)
  (2..spreadsheet.last_row).each do |i|  
      name     = spreadsheet.cell(i,'A')    
      lastname = spreadsheet.cell(i,'B')
      doc_nat  = spreadsheet.cell(i,'C')

      user = User.create(:name => name,
                                 :lastname =>lastname,
                                 :doc_nat =>doc_nat)
      if user.save
        # stuff to do on successful save 
      else
        user.errors.full_messages.each do |message|
          @errors << "Issue line #{i}, column #{message}"
        end
      end

 end    
end

def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
  when ".csv" then Roo::CSV.new(file.path, csv_options: {encoding: "iso-8859-1:utf-8"})
  when ".xls" then  Roo::Excel.new(file.path, packed: false, file_warning: :ignore)
  when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
  else raise "Unknown file type: #{file.original_filename}"
end

end

I'm trying to find the way to show a message indicating the ROW PROBLEM on the index view.

For example I'm adding a user that already exist and want to show a message indicating

ROW 1 already exist
Row 2 is duplicated
Row 1 must be string 
or something like that 

But after uploading the excel just getting numbers and not the message:

Issue line 2, column already exist
Issue line 3, column already exist

Just getting numbers:

2  3
1

There are 1 best solutions below

7
On BEST ANSWER

If the user can't be saved that means it failed validation (although the only validation you have at the moment is on doc_nat... you may want to add validations for the "must be string" conditions, etc).

For an active_record object that failed validation, there's an array object.errors.full_messages which is all the errors found when validating.

So...

def self.import(file)
  @errors = []
  spreadsheet = open_spreadsheet(file)
  (2..spreadsheet.last_row).each do |i|  
    name     = spreadsheet.cell(i,'A')    
    lastname = spreadsheet.cell(i,'B')
    doc_nat  = spreadsheet.cell(i,'C')

    user = User.new(:name => name,
                       :lastname =>lastname,
                       :doc_nat =>doc_nat)
    if user.save
      # stuff to do on successful save 
    else
      user.errors.full_messages.each do |message|
        @errors << "La información de la línea #{i}, columna #{message}"
      end
    end
  end 
  @errors #  <- need to return the @errors array   
end

and in the view...

<% if @errors %>
  <% @errors.each do |message| %>
    <%= message %>
  <% end %>
<% end %>