Rails导入CSV - 无路由匹配

时间:2012-07-13 15:08:57

标签: ruby-on-rails csv methods import

我对Rails很新。我目前正在尝试将数据从csv文件导入到我的rails应用程序中。但是,当我在线跟踪示例和指南时,我收到错误: 没有路由匹配{:action =>“import_csv”,:controller =>“lists”} 我已经在我的routes.rb中添加了它。如果我的代码导致'无路由匹配'错误,任何人都可以帮我检查是什么问题吗?以下是我的文件:

lists_controller.rb

def import_csv

require 'fastercsv' 

respond_to do |format|
@csv=params[:file].read
@n=0
@parsed_file = CSV.parse(csv)
@parsed_file.each  do |row|
  @user_new = User.new
  @user_new.first_name = row[0]
  @user_new.last_name = row[1]
  @user_new.email = row[2]
  @user_new.address = row[3]
  @user_new.city = row[4]
  @user_new.state = row[5]
  @user_new.zip = row[6]
  @user_new.country = row[7]
  @user_new.notes = row[8]
  @user_new.birthday = row[9]
  @user_new.home_number = row[10]
  @user_new.mobile_number = row[11]
  @user_new.list_id = list_id
  @user_new.save
  @n=@n+1
  GC.start if n%50==0
    flash[:notice] = "CSV Imported Successfully, with  #{n} records"                                
end

format.html { redirect_to lists_url }
  format.json { head :no_content }
end
  end

应用程序/视图/列表/ show.html.erb

<%= form_for(:list, :url => list_import_csv_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 %>

的routes.rb

  resources :lists do
    get 'import_csv'
    #match '/import_csv/:id' => 'lists#import_csv', :as => :import_csv
  end

佣金路线

    identities GET    /identities(.:format)                        identities#index
                  POST   /identities(.:format)                        identities#create
     new_identity GET    /identities/new(.:format)                    identities#new
    edit_identity GET    /identities/:id/edit(.:format)               identities#edit
         identity GET    /identities/:id(.:format)                    identities#show
                  PUT    /identities/:id(.:format)                    identities#update
                  DELETE /identities/:id(.:format)                    identities#destroy
newsletter_cancel GET    /newsletters/:newsletter_id/cancel(.:format) newsletters#cancel
      newsletters GET    /newsletters(.:format)                       newsletters#index
                  POST   /newsletters(.:format)                       newsletters#create
   new_newsletter GET    /newsletters/new(.:format)                   newsletters#new
  edit_newsletter GET    /newsletters/:id/edit(.:format)              newsletters#edit
       newsletter GET    /newsletters/:id(.:format)                   newsletters#show
                  PUT    /newsletters/:id(.:format)                   newsletters#update
                  DELETE /newsletters/:id(.:format)                   newsletters#destroy
  list_import_csv GET    /lists/:list_id/import_csv(.:format)         lists#import_csv
            lists GET    /lists(.:format)                             lists#index
                  POST   /lists(.:format)                             lists#create
         new_list GET    /lists/new(.:format)                         lists#new
        edit_list GET    /lists/:id/edit(.:format)                    lists#edit
             list GET    /lists/:id(.:format)                         lists#show
                  PUT    /lists/:id(.:format)                         lists#update
                  DELETE /lists/:id(.:format)                         lists#destroy
            users GET    /users(.:format)                             users#index
                  POST   /users(.:format)                             users#create
         new_user GET    /users/new(.:format)                         users#new
        edit_user GET    /users/:id/edit(.:format)                    users#edit
             user GET    /users/:id(.:format)                         users#show
                  PUT    /users/:id(.:format)                         users#update
                  DELETE /users/:id(.:format)                         users#destroy

2 个答案:

答案 0 :(得分:0)

post

中使用get代替config/routes.rb

答案 1 :(得分:0)

您的表单是通过POST提交的,而路由是用于GET。要么将:method => :get传递给form_for助手,要么将路线更改为post(我更喜欢前者,因为您要求数据,而不是更改服务器上的任何内容)。< / p>

要为表单指定GET方法,您可以执行

<%= form_for (:list, :url => list_import_csv_path, :method => :get, :html => {:multipart => true}) do |f| %>

我也不确定强制启动垃圾收集是否是一个好主意 - 如果它在处理请求时启动它会杀死你的性能。剩下的对象应该自动收集垃圾。

我还会提取用户导出部分(可能是User model上的静态方法):

require 'faster_csv'

class User < ActiveRecord::Base
  ...
  def self.import_from_csv(file)
    CSV.parse(file).each do |row|
      u = User.new(:first_name => row[0], :last_name => row[1] ...etc)
      return false if !u.save
    end
  end
end

然后,在控制器中:

def import_csv
  respond_to do |format|
    if !User.import_from_csv(params[:file])
      format.html { render :show, :error => "Some error here" }
      format.json { render :json => "Some error here", :status => :unprocessable_entity }
    else
      format.html { redirect_to lists_url, :notice => "Import successful!" }
      format.json { :head => :ok }
    end
  end  
end