0

我对 Rails 很陌生。我目前正在尝试将数据从 csv 文件导入到我的 rails 应用程序中。但是,当我在网上按照示例和指南进行操作时,我得到了错误:没有路由匹配 {:action=>"import_csv", :controller=>"lists"}我已经将它添加到我的 routes.rb 中。如果我的代码导致“无路由匹配”错误,谁能帮我检查一下出了什么问题?以下是我的文件:

列表控制器.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 %>

路线.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
4

2 回答 2

0

postgetconfig/routes.rb.

于 2012-07-13T15:13:13.483 回答
0

您的表单是通过 POST 提交的,而路由是针对 GET 的。要么传递:method => :getform_for助手,要么改变你的路线post(我更喜欢前者,因为你正在请求数据,而不是改变服务器上的任何东西)。

要为表单指定 GET 方法,您可以这样做

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

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

我还将提取 User 导出部分(可能到 上的静态方法中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
于 2012-07-13T15:16:43.147 回答