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?
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.
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.
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:
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