hello, i am new to ruby and trying to figure out how to output the following code in view layout. this code resides in one of the controller called X: def get_log @jobname = X.find params[:id] File.open("c:\\test\\#{@jobname.job}.txt") do |file| file.each do |line| puts line end end this code opens the file and prints the file but it does it in the console, i want it to be able to output it through my get_log.rhtml file. How do i go about doing this? I''ve tried including the same code with <%= %> in view but it doesnt work. Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
keep that in controller:> @jobname = X.find params[:id]<% File.open("c:\\test\\#{@jobname.job}.txt") do |file| %> <% file.each do |line| %> <%= line %> <% end %> <% end %> this should work you can save a few characters: <% File.open("c:\\test\\#{@jobname.job}.txt") do |file| file.each do |line| %> <%= line %> <% end end %> but that''s a question of taste, i would go for the first version -- 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 -~----------~----~----~----~------~----~------~--~---
Thank you. this works. Thorsten Mueller wrote:> > keep that in controller: >> @jobname = X.find params[:id] > > > <% File.open("c:\\test\\#{@jobname.job}.txt") do |file| %> > <% file.each do |line| %> > <%= line %> > <% 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 -~----------~----~----~----~------~----~------~--~---
Hi, Welcome to Ruby and Rails! When you''re new to a language like Ruby or a framework like Rails, there''s a great satisfaction in getting something working and it looks like you are well on the way. I''m not suggesting that you change anything in your code but it is worth thinking about the purpose of the model, view and controller. The fact that the content of the job is on the file system is a piece of business logic - who knows how that might change in the future. It should therefore be hidden in one place in the model layer. It would probably be better to add something like a "lines" method which implemented an iterator for the content of the files. Your controller would stay the same but the view would change to be <% @jobname.lines.each do |aLine| %> <%= aLine %> <% end %> better still would be a little helper method which would allow you to do something like this in the view ... <%= display_job_content %> Again, it''s more important at this stage that you get something working but when you have the time, try to think through how the view, controller and models help you to build maintainable code. Good luck! Chris On Jan 29, 7:19 pm, ne mo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thank you. this works. > > Thorsten Mueller wrote: > > > keep that in controller: > >> @jobname = X.find params[:id] > > > <% File.open("c:\\test\\#...@jobname.job}.txt") do |file| %> > > <% file.each do |line| %> > > <%= line %> > > <% end %> > > <% end %> > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Chris, I will keep this in mind. nemo Chris Mccauley wrote:> Hi, > > Welcome to Ruby and Rails! When you''re new to a language like Ruby or > a framework like Rails, there''s a great satisfaction in getting > something working and it looks like you are well on the way. > > I''m not suggesting that you change anything in your code but it is > worth thinking about the purpose of the model, view and controller. > The fact that the content of the job is on the file system is a piece > of business logic - who knows how that might change in the future. It > should therefore be hidden in one place in the model layer. It would > probably be better to add something like a "lines" method which > implemented an iterator for the content of the files. Your controller > would stay the same but the view would change to be > > <% @jobname.lines.each do |aLine| %> > <%= aLine %> > <% end %> > > better still would be a little helper method which would allow you to > do something like this in the view ... > > <%= display_job_content %> > > > Again, it''s more important at this stage that you get something > working but when you have the time, try to think through how the view, > controller and models help you to build maintainable code. > > Good luck! > > Chris-- 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 -~----------~----~----~----~------~----~------~--~---