首页 > 解决方案 > Rails 6 CSV上传错误数量的参数(给定1,预期2)

问题描述

嗨,我有一个具有帐户和客户端模型的 rails 6 应用程序。Client属于_Account

class Client < ApplicationRecord
  belongs_to :account
  has_many :sites, dependent: :destroy
    
  def item_count
    self.sites.items.count
  end
      
  def self.import(file, account_id)
    account = account.find_by(id: account_id)
    CSV.foreach(file.path, headers: true) do |row|
      account.clients.create! row.to_hash
    end
  end
end

class Account < ApplicationRecord
  has_many :clients, dependent: :destroy
end

我想通过表格从帐户显示页面导入 CSV 格式的客户。

<%= form_tag import_clients_path, multipart: true do %>
<%= hidden_field_tag(:account_id, value: @account) %>
<%= file_field_tag :file %>
<%= submit_tag "Import Clients" %>
<% end %> 

路线在此处创建

resources :clients do
  collection {post :import}
end

我不断得到:

ArgumentError in ClientsController#import
wrong number of arguments (given 1, expected 2)
Extracted source (around line #9):
  7
  8
  9
  10
  11
  12

客户控制器

def import
    Client.import(params[:file])
    redirect_to account_path(@account), notice: 'Clients Imported Successfully'
  end

         

这让我发疯了,我相信这是一个简单的错误,感谢您的帮助。

标签: ruby-on-railscsv

解决方案


推荐阅读