将CSV导入添加到ROR应用程序 [英] Adding CSV Import to a ROR application

查看:146
本文介绍了将CSV导入添加到ROR应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在线订购应用程序中为批量订单添加CSV导入。基本上我想要做的是从CSV数据创建一个购物车。

I am adding a CSV import for bulk orders in my online ordering application. Basically what I am looking to do is to create a cart out of the CSV data.

我创建了一个imports_controller:

I created an 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

它会在日志中生成以下错误: / p>

it is generating the following error in the log:

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汇入表单位于快速订购页面:

The CSV Import form is in thequick order page:

    <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| 

为什么不设置参数。我的想法是与形式调用

and why that the param is not being set. My thought is that is has to do with the form call of

<%= f.file_field :file -%>

任何帮助将不胜感激 - 谢谢!

Any help would be appreciated - Thanks!

推荐答案

您的表单正在提交到 csv_import 操作。您的控制器定义了 csv_import = 方法。不确定你的控制器还有什么,或者 csv_import = 方法是什么,但你需要一个 csv_import

Your form is submitting to the csv_import action. Your controller defines a csv_import= method. Not sure what else is in your controller or what that csv_import= method is for, but you need a csv_import method in your controller that can process the request.

这可能是:

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

这篇关于将CSV导入添加到ROR应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆