Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将数据保存到ruby on rails中的CSV文件_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 将数据保存到ruby on rails中的CSV文件

Ruby on rails 将数据保存到ruby on rails中的CSV文件,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我想在RubyonRails中将使用html表单从用户那里获得的一些数据添加到CSV文件中。我该怎么做?这是我的代码 <p> <h2>Register using form below</h2> <p class="error"></p> <form action="" method="post" class="scrum-form"> <p><h3>Participant information&l

我想在RubyonRails中将使用html表单从用户那里获得的一些数据添加到CSV文件中。我该怎么做?这是我的代码

<p>
<h2>Register using form below</h2>
<p class="error"></p>
<form action="" method="post" class="scrum-form">
<p><h3>Participant information</h3></p>
<div>
<label>Full name<span class="mandatory">*</span></label>
<input type="text" name="full_name" class="full_name" />
</div>
<div>
<label>Email<span class="mandatory">*</span></label>
<input type="text" name="email" class="email" />
</div>
<div>    
<label>Phone number<span class="mandatory">*</span></label>
<input type="text" name="phone" class="phone" />
</div>
<div>
<label>Organization name<span class="mandatory">*</span></label>
<input type="text" name="organization" class="organization" />
</div>
<div>
<label>Job title<span class="mandatory">*</span></label>
<input type="text" name="job" class="job" />
</div>
<div>
<label>Special diet<span class="mandatory">*</span></label>
<input type="text" name="diet" class="diet" />
</div>
<p><h3>Billing information</h3></p>
<div>
<label>Address<span class="mandatory">*</span></label>
<textarea cols="40" rows="5" name="address" name="address" class="address"></textarea>
</div>
<div>
<label>Postal code<span class="mandatory">*</span></label>
<input type="text" name="code" class="code" />
</div>
<div>
<label>City<span class="mandatory">*</span></label>
<input type="text" name="city" class="city" />
</div>
<div>
<label>Cost pool or reference<span class="mandatory">*</span></label>
<input type="text" name="cost_pool" class="cost_pool" />
</div>
<div>
<input type="button" name="register" value="Register" onclick="registration();" />
</div>
</form>
</p>

看看rubies CSV库(又名FasterCSV)


或者观看最新的RailsCast:

看看rubies CSV库(又名FasterCSV)


或者观看最新的RailsCast:

使用ruby中的内置csv类,写入csv文件非常简单:


但是,我建议在填写表单时坚持rails范式更新模型(并使用标准数据库),而不是将控制器耦合到此文件。然后,您可以使用rake任务或管理页面,将您想要的数据从数据库导出到csv中。许多人非常喜欢这种功能。

使用ruby中的内置csv类,编写csv文件非常简单:


但是,我建议在填写表单时坚持rails范式更新模型(并使用标准数据库),而不是将控制器耦合到此文件。然后,您可以使用rake任务或管理页面,将您想要的数据从数据库导出到csv中。很多人都非常喜欢这种功能。

我给你举个例子:

@file = "example.csv"

CSV.open(@file, "wb") do |csv|
  csv << ["data"]
  csv << ["data2", "data3"]
end

无论如何,这里有一个链接,其中包含了您可以传递的所有选项:。

我将给您举个例子:

@file = "example.csv"

CSV.open(@file, "wb") do |csv|
  csv << ["data"]
  csv << ["data2", "data3"]
end

无论如何,这里有一个链接,包含您可以传递的所有选项:。

我跟随了您的第一个链接,现在数据被添加到csv文件中。但现在每次我使用该表单添加数据时,它都会在同一行上添加数据,因此它会删除以前保存的数据并在该位置插入新数据。但我想在下一行添加新信息。如何操作?打开文件时,请使用
'a'
而不是
'w'
。我遵循了您的第一个链接,现在数据已添加到csv文件中。但现在每次我使用该表单添加数据时,它都会在同一行上添加数据,因此它会删除以前保存的数据并在该位置插入新数据。但我想在下一行添加新信息。如何执行此操作?打开文件时,请使用
'a'
而不是
'w'
。我遵循了您的代码,现在数据已添加到csv文件中。但现在每次我使用该表单添加数据时,它都会在同一行上添加数据,因此它会删除以前保存的数据并在该位置插入新数据。但我想在下一行添加新信息。我该怎么做呢?我遵循了您的代码,现在数据被添加到csv文件中。但现在每次我使用该表单添加数据时,它都会在同一行上添加数据,因此它会删除以前保存的数据并在该位置插入新数据。但我想在下一行添加新信息。我该怎么做?
   CSV.open(@file, "ab") do |csv|
      csv << ["your_new_next_line_data"]
    end