English 中文(简体)
2. 单体文档中与我sql数据b 相匹配的浏览量和栏目
原标题:Validations for rows and columns in the csv file matching with mysql databse fields

i did the task of importing csv file to MySql databse. But now my requirements are: 1. Rows and columns of csv file should match with rows and columns of MySql database such that if any row or column is missing, it should raise an error: "Missing @row1 or column 2".. 2. In my code, table header i.e. name, location, city etc; headings are often repeating on every import.

我的法典如下:

def load_csv

end

 def import_csv
csv=params[:file].read
n=0
parsed_file = CSV.parse(csv)
parsed_file.each  do |row|
i=User.new
i.name = row[0]
i.location = row[1]
i.city = row[2]
if i.valid?
i.save
n=n+1
GC.start if n%50==0
flash[:notice] = "CSV Imported Successfully, with  #{n} records"                                
end
end

In views:

<%= form_for(:user, :url => import_csv_user_path,  :html => {:multipart => true}) do |f| %>
 <table>
<tr>
<td>
<label for="dump_file">
Select a CSV File :
</label>
</td>
<td ><%= file_field_tag :file -%></td>
</tr>
<tr>
<td colspan= 2 ><%= submit_tag  Submit  -%></td>
</tr>
</table>
<% end -%>

请帮助我

问题回答

我与你目前的工作一样。 然而,我认为,这样做是一种非常好/有远见的前瞻性/想法/可取的解决办法,因为(验证CSV档案)并不是一项非常好的要求,因为我对数据库/模型的“转化控制”也是如此。 这或许是你制定的所有法典,特别是对那些不是专家的人。

OK提出投诉。 请允许我谈谈我的解决方法:

  1. 通过javascript。 我没有以这种方式加以执行,但我倾向于这样做。 步骤:

    1. read local csv file( refer to jasmine s approach to load test fixture).
    2. validate the columns between csv and database table s.
    3. show result. if not pass, give user warning message.
  2. 通过远程垃圾处理法: 我以这种方式这样做,认为这样做并不好(也许我利用了许多支职...... d脏,而不是leg,难以检测,反应迟缓,ja......等) 如果是,仅使用直线的html页,而不是“方言”或“模式窗口”。

    1. update file to remote server.
    2. validate the csv file in remote server.
    3. give user warning message if validation failed.




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签