# File output in view layout

**URL:** <https://www.ruby-forum.com/t/file-output-in-view-layout/127491>\
**Category:** Rails\
**Created:** [January 29, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/file-output-in-view-layout/127491 "2008-01-29T19:37:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nemo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nemo](https://www.ruby-forum.com/u/nemo)\
**Post date:** [January 29, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/file-output-in-view-layout/127491/1 "2008-01-29T19:37:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nemo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nemo](https://www.ruby-forum.com/u/nemo)\
**Post date:** [January 29, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/file-output-in-view-layout/127491/2 "2008-01-29T20:14:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nemo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nemo](https://www.ruby-forum.com/u/nemo)\
**Post date:** [January 29, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/file-output-in-view-layout/127491/3 "2008-01-29T20:19:48Z")

</div>

Thank you. this works.

Thorsten M. 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 %\>

---

<div class="post-metadata">

**Author:** ![nemo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nemo](https://www.ruby-forum.com/u/nemo)\
**Post date:** [January 29, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/file-output-in-view-layout/127491/4 "2008-01-29T20:53:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nemo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f07891/32.png) [@nemo](https://www.ruby-forum.com/u/nemo)\
**Post date:** [February 12, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/file-output-in-view-layout/127491/5 "2008-02-12T19:25:48Z")

</div>

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
