How to generate CSV files in Rails
June 28, 2007 at 12:30 pm 4 comments
Controller code :
require 'csv' def export_to_csv@customers=CustomerInformation.find(:all) report = StringIO.new CSV::Writer.generate(report, ',') do |title| title << ['Id', 'Title', 'Job Title', 'First Name', 'Last Name'] @customers.each do |c| title < 'text/csv; charset=iso-8859-1; header=present',:filename => 'report.csv', :disposition =>'attachment', :encoding => 'utf8') end end end
View:
<% form_tag({ :action => :export_to_csv })do -%> <%= submit_tag "Export To CSV" -%> <% end -%>
4 Comments Add your own
Leave a comment
Trackback this post | Subscribe to the comments via RSS Feed
1. varma | July 16, 2007 at 7:16 pm
how can we apply validation to image uploading file…
2. Satish Chauhan | July 17, 2007 at 5:38 pm
Hello Mr. verma
What type of validation you want ?
for type use
validates_format_of :picture, :with => %r{\.(gif|jpg|png)$}i,
:message => “must be a URL for a GIF, JPG, or PNG image”
for size use
validates_size_of :picture, :maximum =>
If your problem is somthing else, let me know the problem
or you can use file_column plugin
3. varma | July 17, 2007 at 6:23 pm
hai satish, thank for your fast replay
i am using file_column plugin in my application, for uploading image file to database.when i am uploading the image file to database without any imagepath(i.e empty ) in the upload field its getting error as:
undefined method `read’ for “”:String
i want to display the validation message rather than displaying the error message, for my application.so what type of validations i have to apply to my image uploading please can u suggest any thing more….
4. Satish Chauhan | July 17, 2007 at 8:17 pm
dear
file_column did not store file in database , it store its name only
use
{:controller=>”controller_name”, :action=>”action_name”}, :html => { :multipart => true } do |f| -%>
look at html options include :multipart=>true
hope this will definitely solve your problem