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