Exporting to text file with rails

Hi,

I have a list of emails in a users table in my database and my client
wants me to export a list of these users to a format which they can
paste into outlook so that they can bulk email them.

Does anyone have any ideas on the best way to do this? preferably a
download of the file will automatically commence on clicking of a button
or link in one of my html pages.

Cheers,

Chris

On 8 Jan 2008, at 10:27, Chris G. wrote:

or link in one of my html pages.

Well as far as the sending of the data you can use send_data, which
allows you to send any data you want to the client (and you can set
the options so that the browser will download it with the right file
name etc…

Fred

Hi Fred,

I ended up with the following:

def exportArtists
users = Artist.getArtistList
stream_csv do |csv|
csv << [“email”]
users.each do |u|
csv << [u.Email]
end
end
end

private

def stream_csv
filename = params[:action] + “.csv”

 #this is required if you want this to work with IE
 if request.env['HTTP_USER_AGENT'] =~ /msie/i
   headers['Pragma'] = 'public'
   headers["Content-type"] = "text/plain"
   headers['Cache-Control'] = 'no-cache, must-revalidate, 

post-check=0, pre-check=0’
headers[‘Content-Disposition’] = “attachment;
filename=”#{filename}""
headers[‘Expires’] = “0”
else
headers[“Content-Type”] ||= ‘text/csv’
headers[“Content-Disposition”] = “attachment;
filename=”#{filename}""
end

render :text => Proc.new { |response, output|
  csv = FasterCSV.new(output, :row_sep => "\r\n")
  yield csv
}

end

works pretty well and exports the file to .csv format :slight_smile:

Chris

On 8 Jan 2008, at 10:43, Chris G. wrote:

   csv << [u.Email]
if request.env['HTTP_USER_AGENT'] =~ /msie/i

filename="#{filename}""
end

render :text => Proc.new { |response, output|
csv = FasterCSV.new(output, :row_sep => “\r\n”)
yield csv
}
end

Just so you know,

send_data some_data, :type => ‘text/csv’, :filename => filename

Should accomplish most of what you’ve written above.

Fred