Elmor
Elmor

Reputation: 4895

output from a ruby script into web browser

I'm trying to implement http://blog.sosedoff.com/2011/04/09/serving-maintenance-page-with-rack-middleware/ with only one difference - my message is a whole *.html file which is read like so:

def default_prompt(t)
      File.open("public/tmp/maintenance/maintenance.html", "r").read
    end

and output is

 if File.exists?(@file)
    body = @block.nil? ? default_prompt(time_info) : @block.call(time_info)
    res = Response.new
    res.write(body)
    res.finish
  else
    @app.call(env)

But i get text of the html file in the end because output is surrounded by <pre> tags.
How can i solve this problem?

Upvotes: 0

Views: 418

Answers (1)

Christopher WJ Rueber
Christopher WJ Rueber

Reputation: 2161

It just appears that you have <pre> tags surrounding it. What's actually happening is that the result that you're returning isn't a properly formed response to Rack (you need some kind of content headers to indicate what you're sending back). You need to implement something more like this:

if File.exists(@file)
  maintenance_html = File.open(@file, "r").read
  [200, {"Content-Type" => "text/html"}, maintenance_html]  # This is a proper Rack response.
else
  @app.call(env)

inside of your middleware call function.

Upvotes: 2

Related Questions