Hello, I''m fairly new to Ruby on Rails. I have a model called Report and I''m trying to create a text file that is saved in a /public/report directory. I''ve had a look at the ruby api but I can''t seem to get this working. The current code I have is: #report.rb class Report < ActiveRecord::Base after_save :write_file def write_file if @file_data File.makedirs("#{RAILS_ROOT}/report") File.open("#{RAILS_ROOT}/report/waiting.adc", "w") { |file| file.write("hello world") } end end end #report_controller.rb def new @report = File.new end def create if @report.save flash[:notice]=''report'' else render :action=>''new'' end end Thank you for your time. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
try File.makedirs("#{RAILS_ROOT}/public/report") File.open("#{RAILS_ROOT}/public/report/waiting.adc", "w") { | file| file.write("hello world") } end On Nov 13, 4:26 pm, Ryan Mckenzie <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > I''m fairly new to Ruby on Rails. > > I have a model called Report and I''m trying to create a text file that > is saved in a /public/report directory. > > I''ve had a look at the ruby api but I can''t seem to get this working. > > The current code I have is: > > #report.rb > class Report < ActiveRecord::Base > > after_save :write_file > > def write_file > if @file_data > File.makedirs("#{RAILS_ROOT}/report") > File.open("#{RAILS_ROOT}/report/waiting.adc", "w") { |file| > file.write("hello world") } > end > end > end > > #report_controller.rb > > def new > @report = File.new > end > > def create > if @report.save > flash[:notice]=''report'' > else > render :action=>''new'' > end > end > > Thank you for your time. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Jeff, I''ve changed the code to what you have suggested only it has not worked. I can''t seem to find anywhere on Google from searching a solution to this. Is there a better way of writing files to a directory? Thank you for your help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
With Ruby V1.87p72 on MacOS X 10.5 662 > cat junk/newbe cat: junk/newbe: No such file or directory 663 > irb irb(main):001:0> Dir.mkdir("junk") => 0 irb(main):002:0> Dir.chdir("junk") => 0 irb(main):003:0> f = File.new("newbe", "w+") => #<File:newbe> irb(main):004:0> f.write("hello world\n") => 12 irb(main):005:0> f.close => nil irb(main):006:0> quit 664 > cat junk/newbe hello world Rick On Nov 13, 11:26 am, Ryan Mckenzie <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > I''m fairly new to Ruby on Rails. > > I have a model called Report and I''m trying to create a text file that > is saved in a /public/report directory. > > I''ve had a look at the ruby api but I can''t seem to get this working. > > The current code I have is: > > #report.rb > class Report < ActiveRecord::Base > > after_save :write_file > > def write_file > if @file_data > File.makedirs("#{RAILS_ROOT}/report") > File.open("#{RAILS_ROOT}/report/waiting.adc", "w") { |file| > file.write("hello world") } > end > end > end > > #report_controller.rb > > def new > @report = File.new > end > > def create > if @report.save > flash[:notice]=''report'' > else > render :action=>''new'' > end > end > > Thank you for your time. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---