File.open of HTML file removes code

Hey there,

I am trying to edit an erb file in ruby, however when I do:

file.open(“index.erb”)

It displays the information, but removes the Doctype, html, body, and
head tags.

Is there any way to read the entire contents of the .erb file?

Hello,

Please try to run this commands in your OS command prompt:

For Windows:

type index.erb

For Linux/Mac OS X:

cat index.erb

And then:

$ irb
irb(main):001:0> File.open(‘index.erb’).read

Then compare system output with Ruby’s output - if they have no
differences - the problem is somewhere in your code. In that case please
post your code to www.pastebin.com and give a link there.

Good luck. :slight_smile:

Scott Elwood wrote in post #995260:

Hey there,

I am trying to edit an erb file in ruby, however when I do:

file.open(“index.erb”)

It displays the information, but removes the Doctype, html, body, and
head tags.

Is there any way to read the entire contents of the .erb file?

File.open("…") just opens the file and returns an open File object,
but it doesn’t read it until you ask it to.

Try:

File.open(“index.erb”) do |file|
file.each_line do |line|
puts line
end
end

Thanks to both of you!

I checked both of the code you guys suggested and it worked fine.
However there is still a problem.

I am developing an application for a ruby class I am in, and I would
like to edit some of that .erb file in an HTML textarea. I am using
Ruby/Sinatra, and here is what happens:

If I print the data into a

, it will show everything including the
doctype, body, etc.

Yet when I do this:

<% File.open("views/layoutAdmin.erb") do |file| %> <% file.each_line do |line| %> <%= line %> <% end %> <% end %>

The textarea will take away all of the aforementioned tags. Like I said
before, if I replace the tags with

tags, it works fine,
but I of course can’t edit that content.

Is there some reason why textareas do not display this data?

Scott Elwood wrote in post #995373:

Thanks to both of you!

I checked both of the code you guys suggested and it worked fine.
However there is still a problem.

I am developing an application for a ruby class I am in, and I would
like to edit some of that .erb file in an HTML textarea.

You need to post that .erb file. Or, even better, create the most
minimal .erb file you can that will still exhibit your problem.

If I print the data into a

,

Huh? What data? What

?

Is there some reason why textareas do not display this data?

Huh? I thought we were talking about an .erb file? erb files don’t
display anything. If the result of your erb file is an html file, also
post the html file that is produced after ruby processes the erb file.
Browsers display html files, and what a browser does to your html file
is irrelevant at this point. And if this ultimately ends being a
“Why doesn’t my browser display this html properly?” question, then you
have to tell us which browser you are using(and then the ruby forum is
probably not the best place for such a question).

Thanks again for the comment, it led me to test some stuff out and found
it was TinyMCE javascript plugin that was removing the header and such.
I tried testing if that was the case earlier but a refresh of the page
didn’t refresh the code, I had to actually reload the page for it to
reset.

Sorry for the confusion, this is a great forum :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs