generate xml file to download it

hi 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 = File.open("/home/delmed/Bureau/users.xml","a+")   else     file = File.new('/home/delmed/Bureau/users.xml','a+')   end     @user.each do |user|      file.puts "aa"     end     file.close end

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:

http://www.rorexperts.com/send-data-and-send-file-in-rails-t1648.html

You should be able to solve it. :slight_smile:

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

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:

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: spec/views/public/landing.html_spec.rb

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

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

    headers['Content-Type'] = "application/vnd.ms-excel"     headers['Content-Disposition'] = 'attachment; filename="report.xls"'     headers['Cache-Control'] = ''

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

<table border="1">   <tr>     <th>First Name</th><th>Last Name</th>   </tr>   <% @user.each do |u| %>   <tr>    <td><%= user.firstname %></td><td><%= user.lastname %></td>   <% end %> </tr> </table>

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!

Aaron

it work good :slight_smile: thanks

but it dont give me the dialog box to choose location to save the file !!! its normal?

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']    end end

send_data csv_string,           :type => 'text/csv; charset=iso-8859-1; header=present',           :disposition => "attachment; filename=student.csv"

Good luck. :slight_smile: