2012-04-11 32 views
0

我實現了將數據從csv導入MySQL數據庫的任務。現在,我的要求是:如果導入時csv文件中的數據有任何不匹配,我需要使用行號獲取錯誤消息。在Rails 3.1中導入CSV數據到MySql數據庫的驗證?

這是我的控制器代碼: ** require 'csv' #at頂部,其次是...

def load_csv 
# no code 
end 

def import_csv 
parsed_file = CSV.foreach(params[:csv].tempfile,:headers => true) do |row| 
row = row.to_hash.with_indifferent_access 
Institute.create!(row.to_hash.symbolize_keys) 
redirect_to :action => :index 
end 

在我看來/ load_csv.html.erb:

<%= form_for(:institute, :url => import_csv_institutes_path, :html => {:multipart => true}) do |f| %> 
<div class="field"> 
<%= file_field_tag :csv %> 
<%= f.submit 'Import' %> 
</div> 
<% end %> 

這些都是我用於導入的唯一兩個步驟。模型驗證只適用於表單和csv,顯示錯誤頁面。 請儘量幫助我.........................

回答