Formatting issue


#1

I have a text file that has data in it which is perfectly formatted the
way I want it.
I have the following code in my controller to open the file and read
each line into an array which I then call from my view. The problem is,
I completely loose the formatting the way I’m doing it:

  • from my controller:

    def index
    @ssh = Array.new
    a = File.open("/usr/bin/my_data.dat")
    a.each do |line|
    @my_data.push(line)
    end
    end

-from my view:

<%= @my_data %>

I’m thinking that maybe I shouldn’t put the data in an array or maybe I
should somehow loop through the array in my view in order to maintain
the formatting of the text file?

thanks


#2

On Nov 30, 11:36 pm, John V. removed_email_address@domain.invalid
wrote:

I have a text file that has data in it which is perfectly formatted the
way I want it.
I have the following code in my controller to open the file and read
each line into an array which I then call from my view. The problem is,
I completely loose the formatting the way I’m doing it:

<%= … %> basically just calls to_s on whatever the expression in
there evaluates to, and to_s on an Array is just the concatenation of
to_s called on everything in it. If you just want to display data from
the file, why not use @my_data = File.read(…) ?

Fred


#3

On Sun, Nov 30, 2008 at 6:03 PM, John V.
removed_email_address@domain.invalid wrote:

Here is the data, if you put it in a file and then tell Rails to read
it, please see if you can get it to display in the exact same format in
Rails. When I do it, all my columns don’t line up:

They don’t line up in this email, either :slight_smile:

Are you trying to display this in PRE tags or equivalent CSS with a
monospace font?

Your display would probably be more robust if you parsed the file
contents and put it in an actual HTML table, though.

FWIW,

Hassan S. ------------------------ removed_email_address@domain.invalid


#4

Here is the data, if you put it in a file and then tell Rails to read
it, please see if you can get it to display in the exact same format in
Rails. When I do it, all my columns don’t line up:

Mod Ports Card Type Model
Serial No.



1 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45
SAD041509MC
2 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45
SAD041206F6
3 16 16 port 1000mb MTRJ ethernet WS-X6416-GE-MT
SAD04220ML4
4 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45
SAD03465383
5 2 Supervisor Engine 720 (Active) WS-SUP720-3B
SAL092750VP
6 2 Supervisor Engine 720 (Warm) WS-SUP720-3B
SAL09285BW6
7 48 48 port 10/100 mb RJ-45 ethernet WS-X6248-RJ-45
SAD041509MY
8 48 CEF720 48 port 10/100/1000mb Ethernet WS-X6748-GE-TX
SAL09285L96
9 6 Firewall Module WS-SVC-FWM-1
SAD084305Y3

Mod MAC addresses Hw Fw Sw
Status



1 0001.970a.dab0 to 0001.970a.dadf 1.2 5.1(1)CSX 8.5(0.46)ROC
Ok
2 00b0.c2f1.f21c to 00b0.c2f1.f24b 1.2 5.1(1)CSX 8.5(0.46)ROC
Ok
3 00d0.c0ce.f854 to 00d0.c0ce.f863 1.0 5.3(1) 8.5(0.46)ROC
Ok
4 0030.9615.2414 to 0030.9615.2443 1.1 4.2(0.24)VAI 8.5(0.46)ROC
Ok
5 0012.dae4.7a28 to 0012.dae4.7a2b 4.4 8.1(3) 12.2(18)SXE3
Ok
6 0012.dae4.7f80 to 0012.dae4.7f83 4.4 8.1(3) 12.2(18)SXE3
Ok
7 0001.974a.0a30 to 0001.974a.0a5f 1.2 5.1(1)CSX 8.5(0.46)ROC
Ok
8 0014.f27e.b460 to 0014.f27e.b48f 2.2 12.2(14r)S5 12.2(18)SXE3
Ok
9 0003.3234.bb36 to 0003.3234.bb3d 3.0 7.2(1) 2.3(1)
Ok

Mod Sub-Module Model Serial Hw
Status



5 Policy Feature Card 3 WS-F6K-PFC3B SAL09253YMB 2.1
Ok
5 MSFC3 Daughterboard WS-SUP720 SAL0926479K 2.3
Ok
6 Policy Feature Card 3 WS-F6K-PFC3B SAL09274WMF 2.1
Ok
6 MSFC3 Daughterboard WS-SUP720 SAL09169JJE 2.3
Ok
8 Centralized Forwarding Card WS-F6700-CFC SAL09316RQD 2.0
Ok

Mod Online Diag Status


1 Pass
2 Pass
3 Pass
4 Pass
5 Pass
6 Pass
7 Pass
8 Pass
9 Pass

thanks for the help,

vic


#5

On 1 Dec 2008, at 11:44, John V. wrote:

@my_data = /usr/bin/my_data.rb #this causes data to be skewed
#@my_data = File.open("/usr/bin/my_data.dat").read #this works
end

index.rthml code:

<%= simple_format @my_data %>

puts always appends a carriage return whereas write doesn’t. if you
changed it to STDOUT.write(line) it should be the same

Fred


#6

Hassan S. wrote:

They don’t line up in this email, either :slight_smile:
…hehe…I know, I’m sorry that wasn’t much of a help :slight_smile:

Are you trying to display this in PRE tags or equivalent CSS with a
monospace font?

…good call on this one though, I added PRE tags on my index.rhtml and
the data is looking the way I want it…which is great news.

I am still a bit baffled as to why I have to write the data to a file
first, then open the file and put it in PRE tags as opposed to using
PUTS statements in my original script and assigning that to a variable
which gets called by my index page which still causes the format to be
skewed.

my_data.rb code:
data.each do |line|
a.write(line) #–this writes to my_data.dat
puts line #–this puts the same data to STDOUT
end

controller code:
def index
@my_data = /usr/bin/my_data.rb #this causes data to be skewed
#@my_data = File.open("/usr/bin/my_data.dat").read #this works
end

index.rthml code:

<%= simple_format @my_data %>

Perhaps the way I’m writing to STDOUT in my_data.rb is causing the
skewing of my data?

thanks

vic


#7

STDOUT.write(line) fixed the problem…thanks again for the help!

Frederick C. wrote:

On 1 Dec 2008, at 11:44, John V. wrote:

@my_data = /usr/bin/my_data.rb #this causes data to be skewed
#@my_data = File.open("/usr/bin/my_data.dat").read #this works
end

index.rthml code:

<%= simple_format @my_data %>

puts always appends a carriage return whereas write doesn’t. if you
changed it to STDOUT.write(line) it should be the same

Fred