這是關於表單中的字段中,我使用csv文件解析和上傳給錯誤在ROR
<%= f.file_field :file ,:url=>{:controller=>"retailers",:action=>"csv_import"}%>
以下是控制器代碼
def create
@retailer = Retailer.new(params[:retailer])
respond_to do |format|
if verify_recaptcha(:model =>@retailer) && @retailer .save
# To notify newly registered user.
retailer_ids = [@retailer.id]
Emailer.on_notify_retailer(retailer_ids, 1, 0)
sign_in @retailer
format.html { redirect_to pages_about_path}
flash[:notice1] = "Thank you for registering with Chindi."
flash[:notice2] = "We will process your application and get back to you within 48 hours. Once approved, you will be able to create negotiable deals that Consumers can tailor to their needs."
flash[:notice3] = "You will be able to create, manage and administer your deals and your buyers in this easy to use control panel."
format.json { render json: pages_about_path, status: :created, location: @retailer }
else
@title = "Sign up"
format.html { render action: "new"}
flash[:notice1] = "Incorrect word verification. Are you sure you\'re human?"
format.json { render json: @retailer.errors, status: :unprocessable_entity }
end
end
csv_parse()
end
上面的代碼被用來保存數據進入數據庫。 CSV文件@retailer.file_file_name
是因爲它需要被解析到被存儲在數據庫以及和值需要存儲在字段
- csv_parse用來解析csvfile
- 我能夠保存文件數據
現在我需要解析csv文件並將各個字段存儲在另一個數據庫中。
csv_parse的代碼如下。
def csv_parse
@parsed_file=CSV.foreach(params[:dump][:file].original_filename)
n=0
@parsed_file.each do |row|
User_list.create(
:email=>row[0],
:first_name=>row[1],
:last_name=>row[2]).save
flash.now[:message]="CSV parse Successful, #{n} new records added to data base"
end
end
當我運行它時,它給出了以下錯誤/秒。
你有沒有對象,當你沒有想到它!
您可能已經預期了Array的一個實例。
的錯誤,而評估零發生。[]
請幫我上面的錯誤爲什麼會拋出這樣的錯誤
在此先感謝。
只是檢查,但你有檢查'params'確認':dump'和':dump [:file]'實際上是定義? – rjz 2012-03-17 16:10:01
@rjz我對軌道上的紅寶石是全新的,我無法理解你想說什麼,請你更具體。非常感謝。 – shofee 2012-03-19 04:50:39
@rjz請給出詳細的信息:dump – shofee 2012-03-19 06:09:44