file output in view layout

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.

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

Thank you. this works.

Thorsten Mueller 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

Thanks Chris,

I will keep this in mind.

nemo

Chris Mccauley wrote: