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 -%>
About these ads

Entry filed under: CSV, Rails, ror. Tags: .

Hello world! Multiple Checkboxes with HABTM

4 Comments Add your own

  • 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

June 2007
M T W T F S S
    Jul »
 123
45678910
11121314151617
18192021222324
252627282930  

Most Recent Posts


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: