11155
(-- --)
August 26, 2011, 8:22pm
1
Hi everybody,
I ve got a problem with strings and the class File in rails
Indeed, when i try to do this:
File.new("archives/sequences/archives-2011.xml")
it works perfectly, however when i do
string = "archives/sequences/" + @filename => filename being
"archives-2011.xml"
File.new(string)
it doesn't work!
ruby tells me it cannot find the file
does anyone could tell me why?
Thanks
Output the value of "string" and see what it is.
Hi everybody,
I ve got a problem with strings and the class File in rails
Indeed, when i try to do this:
File.new("archives/sequences/archives-2011.xml")
it works perfectly, however when i do
string = "archives/sequences/" + @filename => filename being
"archives-2011.xml"
File.new(string)
it doesn't work!
ruby tells me it cannot find the file
Show us the exact error message. Copy and paste it into the post,
don't retype it.
Colin
11155
(-- --)
August 26, 2011, 9:09pm
4
No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
Extracted source (around line #1 ):
1: <h1>Résultat</h1>
<p>id :<%= @result %>
<% r = Result.find(@result ).nameFile %>
<% string = "./archives/results/" + r %>
<% f = File.open(string) %>
Rails.root: /home/thelo/RoR/ihm_test
Application Trace | Framework Trace | Full Trace
app/views/pages/print_result.html.erb:1:in `initialize'
app/views/pages/print_result.html.erb:1:in `open'
app/views/pages/print_result.html.erb:1:in
`_app_views_pages_print_result_html_erb__106515637_82305240_211210089'
11155
(-- --)
August 26, 2011, 9:11pm
5
thelo.g thelo wrote in post #1018716:
Hi everybody,
I ve got a problem with strings and the class File in rails
Indeed, when i try to do this:
File.new("archives/sequences/archives-2011.xml")
it works perfectly, however when i do
string = "archives/sequences/" + @filename => filename being
"archives-2011.xml"
File.new(string)
it doesn't work!
ruby tells me it cannot find the file
You lie:
str1 = 'archives/sequences/archives-2011.xml'
@filename = 'archives-2011.xml'
str2 = "archives/sequences/" + @filename
puts str1 == str1
--output:--
true
11155
(-- --)
August 26, 2011, 9:14pm
6
And the output of the string is:
ActionView::Template::Error (No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
11155
(-- --)
August 26, 2011, 9:23pm
7
@7stud no I do not, I might have bad explained the problem. the thing is
when i perform the following code:
No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
Extracted source (around line #1 ):
<% r = Result.find(@result ).nameFile %>
<% string = "./archives/results/" + r %>
<% f = File.open(string) %>
I got this error:
No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
Extracted source (around line #1 ):
1: <h1>Résultat</h1>
<p>id :<%= @result %>
<% r = Result.find(@result ).nameFile %>
<% string = "./archives/results/" + r %>
<% f = File.open(string) %>
Rails.root: /home/thelo/RoR/ihm_test
Application Trace | Framework Trace | Full Trace
app/views/pages/print_result.html.erb:1:in `initialize'
app/views/pages/print_result.html.erb:1:in `open'
app/views/pages/print_result.html.erb:1:in
`_app_views_pages_print_result_html_erb__106515637_82305240_211210089'
and in the console
ActionView::Template::Error (No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
So when i replace string by "Archives-2011-08-26-09-56-00-UTC.xml"
everything is ok, no problem.
I just want to understand why and fix this problem
Thanks
full patch of "archives" is? if under rails root, then change code to
string = Rails.root.to_s+'/archives/results/'+r
or write down full path to archives directory
tom
11155
(-- --)
August 26, 2011, 9:51pm
9
I tried but it doesn't work, is it possible that the mistake has a ling
with \n at the end of r? because when I do:
puts '"' + r + '"'
I get
"Archives-2011-08-26-09-56-00-UTC.xml
"
11155
(-- --)
August 26, 2011, 9:51pm
10
thelo.g thelo wrote in post #1018731:
@7stud no I do not, I might have bad explained the problem. the thing is
when i perform the following code:
No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
Extracted source (around line #1 ):
<% r = Result.find(@result ).nameFile %>
<% string = "./archives/results/" + r %>
<% f = File.open(string) %>
I got this error:
No such file or directory -
./archives/results/Archives-2011-08-26-09-56-00-UTC.xml
Extracted source (around line #1 ):
1: <h1>Résultat</h1>
<p>id :<%= @result %>
<% r = Result.find(@result ).nameFile %>
<% string = "./archives/results/" + r %>
So when i replace string by
"Archives-2011-08-26-09-56-00-UTC.xml"
('./archives/results' + r) is not the same thing as
"Archives-2011-08-26-09-56-00-UTC.xml"
You have proved nothing.
11155
(-- --)
August 26, 2011, 9:57pm
11
I meant when i replace string by
"./archives/results/Archives-2011-08-26-09-56-00-UTC.xml", which is
normally similar to './archives/results' + r
I tried but it doesn't work, is it possible that the mistake has a ling
with \n at the end of r? because when I do:
puts '"' + r + '"'
I get
"Archives-2011-08-26-09-56-00-UTC.xml
"
\n doesn't matter
ruby-1.8.7-p174 :006 > File.new("./testfile",'w')
=> #<File:./testfile>
ruby-1.8.7-p174 :007 > File.new("./testfile\n",'w')
=> #<File:./testfile
you just cripple the filename ..
11155
(-- --)
August 26, 2011, 10:06pm
13
What do you mean? and if this is the case, how can I fix it?
11155
(-- --)
August 27, 2011, 12:54am
14
thelo.g thelo wrote in post #1018739:
I tried but it doesn't work, is it possible that the mistake has a ling
with \n at the end of r? because when I do:
puts '"' + r + '"'
I get
"Archives-2011-08-26-09-56-00-UTC.xml
"
It's not only possible, that is exactly what is at the end of r. When
you are having such a problem, it is easier to see what the string
actually contains by doing one of the following:
1) puts "-->#{some_var}<---"
2) p r
which is the same as:
puts r.inspect
Here is an example:
str = "hello
world"
p str
--output:--
"hello\nworld"
And of course the newline makes a difference in the file name. You can
test that yourself:
some_file = "html.htm"
File.open(some_file) do |f|
puts f.read
end
Once you get that to work with a file on your file system, try adding a
\n to the end.
11155
(-- --)
August 27, 2011, 1:03am
15
Tom Meinlschmidt wrote in post #1018745:
\n doesn't matter
ruby-1.8.7-p174 :006 > File.new("./testfile",'w')
=> #<File:./testfile>
ruby-1.8.7-p174 :007 > File.new("./testfile\n",'w')
=> #<File:./testfile
you just cripple the filename ..
Really?? I don't see a crippled filename.
..
.
.
.
.
.
.
Luis_M
(Luis M)
August 27, 2011, 7:01am
16
=> #<File:./testfile
you just cripple the filename …
Really?? I don’t see a crippled filename.
You don’t see that > is in the next line? the \n char is being interpreted.
Just do: string.chomp
This should work (as suggested before):
string = Rails.root.to_s+'/archives/results/'+r.chomp
If you really want to be portable you should be writing file paths like: File.join(Rails.root,“archives”,“results”,r.chomp)
11155
(-- --)
August 27, 2011, 7:52am
17
Thank you guys a lot, 7stud was right, I added chomp and it works!
Hence, \n makes a difference.
11155
(-- --)
August 27, 2011, 11:28am
18
Luis Mondesi wrote in post #1018782:
=> #<File:./testfile
>>
>
> you just cripple the filename ..
>
Really?? I don't see a crippled filename.
You don't see that > is in the next line?
Yes, I see exactly that.
the \n char is being
interpreted.
No kidding.