I want to render a view to a file that gets stored on my hard disk. I''ve created an app that allows me to automate e-learning content generation. I perform basic data entry and select one of many HTML content templates I''ve created. Rendering the template with the merged data is no problem, but how can I then write the raw HTML to disk as a file instead of rendering it as a view that the user sees in their browser? Thanks in advance, -- 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 -~----------~----~----~----~------~----~------~--~---
Corey Murphy wrote:> I want to render a view to a file that gets stored on my hard disk. > > I''ve created an app that allows me to automate e-learning content > generation. I perform basic data entry and select one of many HTML > content templates I''ve created. Rendering the template with the merged > data is no problem, but how can I then write the raw HTML to disk as a > file instead of rendering it as a view that the user sees in their > browser? > > Thanks in advance,I''m not sure about full templates, but this should work for partials content = render :partial => ''mypartial'', :locals => { :foo => bar } File.open(''path/to/file.html'', ''r+'') do |f| f.write content end -- 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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne wrote:> I''m not sure about full templates, but this should work for partials > > content = render :partial => ''mypartial'', :locals => { :foo => bar } > File.open(''path/to/file.html'', ''r+'') do |f| > f.write content > endTried with no luck. Never got the file to write to disk. Rails complained about some dynamic constant assignment error which I couldn''t figure out. When I render the page as a full template, it displays without error, so why changing it to a partial and doing what you suggested causes a problem I don''t understand. -- 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 -~----------~----~----~----~------~----~------~--~---
Corey Murphy wrote:> Alex Wayne wrote: >> I''m not sure about full templates, but this should work for partials >> >> content = render :partial => ''mypartial'', :locals => { :foo => bar } >> File.open(''path/to/file.html'', ''r+'') do |f| >> f.write content >> end > > Tried with no luck. Never got the file to write to disk. Rails > complained about some dynamic constant assignment error which I couldn''t > figure out. When I render the page as a full template, it displays > without error, so why changing it to a partial and doing what you > suggested causes a problem I don''t understand.Because "render :partial" returns a string, right then and there. All other types of rendering to my knowledge trigger the rendering engine to send something to the browser. Besides, I think there is an even easier way. Page caching. Page caching essentially does what you are describing. Render a view, save that result to a file. Try, in your controller: caches_page :foo def foo @foo = Foo.find(params[:id]) ... end Now go to /my_controller/foo. Now look in public/my_controller/foo.html and you should see what was just rendered. This is designed to caches content as static files so Rails only has to dynamically generate this stuff once. I dont know if that fits your use case or not, but it should work. -- 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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne wrote:> Because "render :partial" returns a string, right then and there. All > other types of rendering to my knowledge trigger the rendering engine to > send something to the browser. > > Besides, I think there is an even easier way. Page caching. Page > caching essentially does what you are describing. Render a view, save > that result to a file. > > Try, in your controller: > > caches_page :foo > > def foo > @foo = Foo.find(params[:id]) > ... > end > > Now go to /my_controller/foo. Now look in public/my_controller/foo.html > and you should see what was just rendered. This is designed to caches > content as static files so Rails only has to dynamically generate this > stuff once. I dont know if that fits your use case or not, but it > should work.Ok, that worked, so I''m a little closer to my desired outcome. Only problem I see is I need to be able to control where that content gets stored and the filenames that are assigned to the cached pages. I simply need greater control over those elements and I don''t think this method (after reading through the API) is going to allow me to do so. I really appreciate your help thus far though as this has opened my eyes to more rails functionality. -- 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 -~----------~----~----~----~------~----~------~--~---
Corey Murphy wrote:> Ok, that worked, so I''m a little closer to my desired outcome. Only > problem I see is I need to be able to control where that content gets > stored and the filenames that are assigned to the cached pages. I > simply need greater control over those elements and I don''t think this > method (after reading through the API) is going to allow me to do so. I > really appreciate your help thus far though as this has opened my eyes > to more rails functionality.Maybe an after_filter? after_filter :write_file def foo @foo = Foo.find(params[:id]) ... end protected def write_file File.open(''path/to/file'', ''r+'') do |f| f.write response.body end end -- 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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne wrote:> Maybe an after_filter? > > after_filter :write_file > > def foo > @foo = Foo.find(params[:id]) > ... > end > > protected > def write_file > File.open(''path/to/file'', ''r+'') do |f| > f.write response.body > end > endWent back to the original idea of rendering partial to variable and then writing variable to file. Solved the problem related to the error I was receiving thanks to Fred''s post and I think I''m back on track. Thanks for all the ideas and 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 -~----------~----~----~----~------~----~------~--~---
So I''ve read everything I can find on file I/O and for some reason I''m only able to write the contents of the partial to a file that already exists. When I try to create a new file using either "File.new" or "File.open" both return the same error of "No such file or directory . . .". I''ve tried both below with no luck; same error. I''ve also tried with an absolute path including drive letter, etc. since I''m only running a localhost webbrick server at the moment with the same results. content = render :partial => "foo" File.open("#{RAILS_ROOT}/public/generatedHTML/#{id}.html", "wb") do |f| f.write(content.read) end content = render :partial => "foo" File.open("#{RAILS_ROOT}/public/generatedHTML/#{id}.html", "r+") do |f| f.write(content) end So why is it if the file exists, I can write to it, but if it doesn''t exist, neither the create or open methods claim to be able to create/find the file? -- 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 -~----------~----~----~----~------~----~------~--~---
Corey Murphy wrote:> So I''ve read everything I can find on file I/O and for some reason I''m > only able to write the contents of the partial to a file that already > exists. When I try to create a new file using either "File.new" or > "File.open" both return the same error of "No such file or directory . . > .". I''ve tried both below with no luck; same error. I''ve also tried > with an absolute path including drive letter, etc. since I''m only > running a localhost webbrick server at the moment with the same results. > > content = render :partial => "foo" > File.open("#{RAILS_ROOT}/public/generatedHTML/#{id}.html", "wb") do |f| > f.write(content.read) > end > > > content = render :partial => "foo" > File.open("#{RAILS_ROOT}/public/generatedHTML/#{id}.html", "r+") do |f| > f.write(content) > end > > > So why is it if the file exists, I can write to it, but if it doesn''t > exist, neither the create or open methods claim to be able to > create/find the file?I think that was my typo. I believe you want: File.open("#{RAILS_ROOT}/public/generatedHTML/#{id}.html", "w+") Note the "w+" not the "r+". That argument is the file mode. The r or w stands for read or write, and I believe the "+" means create the file if it does not exist. So "w+" means open a file for writitng, and create it if it doesnt exist. Note that the directory you put that file does need to be created however. -- 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 -~----------~----~----~----~------~----~------~--~---
Alex Wayne wrote:> I think that was my typo. I believe you want: > > File.open("#{RAILS_ROOT}/public/generatedHTML/#{id}.html", "w+") > > Note the "w+" not the "r+". That argument is the file mode. The r or w > stands for read or write, and I believe the "+" means create the file if > it does not exist. So "w+" means open a file for writitng, and create > it if it doesnt exist. > > Note that the directory you put that file does need to be created > however.That did it. The "Programming Ruby" book does a horrible job of really digging into File I/O. I was using the "r+" for reading and writing under the assumption that using the "File.open..." syntax using a block was the combined equivalent of File.new/open so I was expecting it to create the file for me as well. The class/module definitions section of the book provides the mode strings for I/O. r+ = Read/Write, starts at beginning of file. w+ = Read/Write, truncates existing file to zero length or creates a new file for reading and writing. Thanks a bunch once again! -- 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 -~----------~----~----~----~------~----~------~--~---
Is there any way to bypass the double render checking? Now that I''m able to get my content pages written to disk, I need to be able to write a lot of pages to disk via a loop. I''m using the "render partial to variable, write variable to file" approach. Unfortunately, the render partial still renders the result to the browser and subsequent loops through my recordset to write additional screens to disk is resulting in the double render gotcha. Any ideas? This kind stuff is easy to facilitate in ASP and other web languages, but this is proving to be difficult with rails. -- 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 -~----------~----~----~----~------~----~------~--~---
On May 30, 4:56 pm, Corey Murphy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Is there any way to bypass the double render checking? Now that I''m > able to get my content pages written to disk, I need to be able to write > a lot of pages to disk via a loop. I''m using the "render partial to > variable, write variable to file" approach. Unfortunately, the render > partial still renders the result to the browser and subsequent loops > through my recordset to write additional screens to disk is resulting in > the double render gotcha.Could you just use render_to_string instead ? Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On May 30, 4:56�pm, Corey Murphy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Is there any way to bypass the double render checking? �Now that I''m >> able to get my content pages written to disk, I need to be able to write >> a lot of pages to disk via a loop. �I''m using the "render partial to >> variable, write variable to file" approach. �Unfortunately, the render >> partial still renders the result to the browser and subsequent loops >> through my recordset to write additional screens to disk is resulting in >> the double render gotcha. > > Could you just use render_to_string instead ? > > FredYou are simply put "a plethora of knowledge". Worked like a charm. It''s amazing how this stuff is in my Programming Ruby bible and I still hadn''t stumbled across it until you mentioned it. Brilliant! Thanks once again, -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---