Om3ga
Om3ga

Reputation: 32823

Save data to CSV file in ruby on rails

I want to add some data which I get from user using html form to CSV file in ruby on rails. How can I do this? Here is my code for this

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

here is my action code

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"]

Upvotes: 1

Views: 12946

Answers (3)

Lucas Nogueira
Lucas Nogueira

Reputation: 541

I will give you an example:

@file = "example.csv"

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

Here we have a csv file and we open it with CSV.open. We have to add inside the csv always arrays with some data. Each time we call csv << array will be a new line inside the csv file. Also, the elements inside the array will be separated by commas inside the file.

So, returning to the example, or "example.csv" file will have "data" on it's first line, and "data2,data3" on it's second line.

Check the links @phoet posted on his answer. Will help you understand the example and anything else you may need.

EDITED

You can solve your comments problem by changing the "wb" inside CSV.open call.

If you pass "ab" instead I belive it will solve your problem, like this:

   CSV.open(@file, "ab") do |csv|
      csv << ["your_new_next_line_data"]
    end

Anyway, here is a link with all the options you can pass: http://ruby-doc.org/core-1.9.3/IO.html.

Upvotes: 6

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

Writing to csv files is straightforward with the built-in CSV class in ruby: http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html

Instead of coupling your controller to this file, however, I'd recommend the approach of sticking to the rails paradigm of updating your model (and using a standard database) when the form is filled out. Then, you can have rake tasks or admin pages that can export the data you want from your database into csv. Many people really like ActiveAdmin for this kind of functionality.

Upvotes: 1

phoet
phoet

Reputation: 18835

have a look at rubies CSV library (aka FasterCSV) http://apidock.com/ruby/CSV

or watch one of the latest railscasts: http://railscasts.com/episodes/362-exporting-csv-and-excel

Upvotes: 4

Related Questions