Generate xml file to download it

i want to do a button_to to generate a xml file with all users i ahve
in the DB.
in the view i do:
<%= button_to “generate xml”, :action => “create_file” %>
in the controller :
def create_file
@users = User.find(:all)
if File.exist?("/home/delmed/Bureau/users.xml")
file ="/home/delmed/Bureau/users.xml",“a+”)
file =’/home/delmed/Bureau/users.xml’,‘a+’)
@user.each do |user|
file.puts “aa”

when i click the button nothing happen and i have any no error !!!
then its possible on click it build a window to allow to choose the
dirictory to save the file?

Try reading the following:

You should be able to solve it. :slight_smile:

Your code just creates/opens /home/delmed/Bureau/users.xml on your
To make it downloadable use send_data or send_file as in the example

You can start by

@users = User.find(:all)
and do a @users.to_xml as the output data on the example found on the
link above.

Good luck. :slight_smile:

thanks man
i will take a look and try to solve it :slight_smile:

2011/5/26 Cris F. [email protected]

Posted via

Mediouni Adel

I am new to rails as well, so you might take my advise with a grain of
salt, but I needed a similar feature myself this very evening so I
found a method of adding the following to a method in one of your

headers['Content-Type'] = "application/"
headers['Content-Disposition'] = 'attachment;

headers[‘Cache-Control’] = ‘’

Then in the corresponding view you simply put a table with all your
data in it… for example:

<% @user.each do |u| %> <% end %>
First Name Last Name
<%= user.firstname %> <%= user.lastname %>

When you visit that page it will promt for a download… so if you were
to simply link to that page it would bring up a download prompt.

Its quick and dirty… but I have no idea if it is the best way to do


it work good :slight_smile: thanks
but it dont give me the dialog box to choose location to save the file
its normal?

2011/5/26 Adel M. [email protected]

You should be able to solve it. :slight_smile:
link above.
To unsubscribe from this group, send email to

Mediouni Adel

Hi Guys,

Still fairly new to Rails. Want to see if I’m doing anything
un-idiomatic. Few questions of style:

  1. “public controller”
    I’m using a “public” controller. It seems to be a good semantic choice,
    because it’s for the landing page and the other few pages that public
    site visitors go to. I see “home” more often, but while home is
    meaningful for the landing page, it doesn’t seem a great word to
    describe the collection of pages accessible to site visitors who have
    not registered (/, about_us, tesrms_and_conditions, etc), hence my
    choice of “public” which seems to better fit what I’m trying to express.
    Weird or just different?

  2. rspec view naming
    I know the default naming convention for a views/public/landing.html.erb
    is spec/views/public/landing.html.erb_spec.rb
    This seems like a bad idea. I don’t want to test that it’s an erb file -
    I want to test that the landing.html page contains whatever I want it to
    contain. So if I move from erb to haml that should not require me to
    rewrite or rename my test. As such, a better convention seems to be:

Any thoughts/opinions? I know it’s small stuff, but I’m trying to
balance idioms with standard best practices with what seems to me to be
the most readable and DRYest approach to writing the code.

Best Wishes,

require ‘fastercsv’

csv_string = FasterCSV.generate do |csv|
csv << [‘admission_no’,‘student_name’,‘contact_no’]
student = Student.find(:all)
student.each do |r|
csv << [r.admission_no,r.student_name,r.contact_no’]

send_data csv_string,
:type => ‘text/csv; charset=iso-8859-1; header=present’,
:disposition => “attachment; filename=student.csv”

Good luck. :slight_smile: