我想将数据附加到 csv 文件。我使用 html 表单从用户那里获取数据。我当前的代码将数据添加到 csv 文件,但不追加。因此,当用户输入他们的详细信息点击注册按钮时,它会删除之前添加的数据,然后在该位置插入新数据。那么我该如何追加新数据,以便将其保存在下一行并将其余数据保留在原位?
这是我正在做的。
full_name = params["full_name"]
email = params["email"]
phone = params["phone"]
organization = params["organization"]
job = params["job"]
diet = params["diet"]
address = params["address"]
code = params["code"]
city = params["city"]
cost_pool = params["cost_pool"]
CSV.open("participants_info.csv", "wb") do |csv|
csv << [full_name, email, phone, organization, job, diet, address, code, city, cost_pool]
end
最佳答案
``a'' Write-only, starts at end of file if file exists, otherwise creates a new file for writing.
``a+'' Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing
使用 CSV.open("participants_info.csv", "a+")
关于ruby-on-rails - 如何将数据附加到csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11426732/