我在在线订购应用程序中为批量订单添加了CSV导入。基本上我要做的是用CSV数据创建一个购物车。
我创建了一个imports_controller:
require 'csv'
class ImportsController < ApplicationController
def csv_import
num_imported = 0
num_failed = 0
CSV.foreach(params[:dump][:file].csv) do |row|
c = CsvImport.new(
cart_items: row[1],
cart_items_quantity: row[2],
cart_items_price: row[3],
cart_items_description: row[4],
cart_items_upc: row[5],
cart_items_sku: row[6]
)
if c.save
num_imported += 1
else
num_failed += 1
end
end
flash.now[:message] = "CSV Import Successful, #{num_imported} new records added to data base. #{num_failed} failed to import."
end
end
它在日志中生成以下错误:
NameError (uninitialized constant ImportsController::CsvImport):
app/controllers/imports_controller.rb:11:in `block in csv_import'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1761:in `each'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1202:in `block in foreach'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1340:in `open'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1201:in `foreach'
app/controllers/imports_controller.rb:10:in `csv_import'
CSV导入表单位于快速订购页面中:
<div class="cart_items column_headers" id="cart_items_add">
<div class="cart_items_description">
<div>
<h3>Import CSV Order</h3>
<% form_for :dump, :url=>{:controller=>"imports", :action=>"csv_import"}, :html => { :multipart => true } do |f| -%>
<table">
<tr>
<td>
<label for="dump_file">
Select a CSV File :
</label>
</td>
<td >
<%= f.file_field :file -%>
</td>
</tr>
<tr>
<td colspan='2'>
<%= submit_tag 'Submit' -%>
</td>
</tr>
</table>
<% end -%>
</div>
</div>
</div>
<% total = 0 %>
<% if !@cart_contents.blank? && !@cart_contents.count.blank? %>
<% @cart_contents.each do |item| %>
<%= render :partial => "my_cart/partials/cart_item", :locals => { :cart_item => item } %>
<% total += (item[:price].to_f * item[:quantity].to_i) %>
<% end %>
<% end %> <!-- if cart is not empty -->
<div class="cart_items">
<div class="cart_items_description">
<br style="clear:both;" />
</div>
<div class="cart_items_quantity" style="padding-top: 6px;">
<p>Total:</p>
</div>
<div class="cart_items_price">
<br style="clear:both;" />
</div>
<div class="cart_items_total">
<p>
<%= text_field_tag("order_total", '%.2f' % total, :readonly => true, :size => "10", :class => "readonly", :style => "color: #C31E22; text-align: right; font-weight: bold;") %>
</p>
</div>
</div>
<div class="cart_items">
<div class="cart_items_full">
<a href="/products"><%= image_tag "/images/continue_shopping.png" %></a>
<a href="/my-cart"><%= image_tag "/images/proceed_to_cart.png" %></a>
</div>
</div>
<br style="clear:both;" />
</div>
<% end %>
*my app running really old version of Ruby and Rails (Ruby 1.9 and Rails 3.2)
目前的问题是
CSV.foreach(File.open params[:dump][:file]) do |row|
为什么没有设置参数。我的想法是与
的表单调用有关<%= f.file_field :file -%>
任何帮助将不胜感激 - 谢谢!
答案 0 :(得分:1)
您的表单正在提交csv_import
操作。您的控制器定义了csv_import=
方法。不确定控制器中还有什么或csv_import=
方法的用途,但您需要在控制器中使用csv_import
方法来处理请求。
或许这样的事情:
def csv_import
num_imported = 0
num_failed = 0
CSV.foreach(params[:file].tempfile) do |row|
c = CsvImport.new(
cart_items: row[1],
cart_items_quantity: row[2],
cart_items_price: row[3],
cart_items_description: row[4],
cart_items_upc: row[5],
cart_items_sku: row[6]
)
if c.save
num_imported += 1
else
num_failed += 1
end
end
flash.now[:message] = "CSV Import Successful, #{num_imported} new records added to data base. #{num_failed} failed to import."
end