How to Access HTML Request Parameters for a .Rhtml Page Served by Webrick

How to access html request parameters for a .rhtml page served by webrick?

According to the source code of erbhandler it runs the rhtml files this way:

    Module.new.module_eval{
meta_vars = servlet_request.meta_vars
query = servlet_request.query
erb.result(binding)
}

So the binding should contain a query (which contains a hash of the query string) and a meta_vars variable (which contains a hash of the environment, like SERVER_NAME) that you can access inside the rhtml files (and the servlet_request and servlet_response might be available too, but I'm not sure about them).

If that is not the case you can also try querying the CGI parameter ENV["QUERY_STRING"] and parse it, but this should only be as a last resort (and it might only work with CGI files).

Mongrel: how to process erb in .rhtml file

After looking through the code of the DirHandler class in Mongrel, it seems that this class is not made for applying any processing to a file, but just serving it up as is.

It seems the only way to do this in Mongrel would be to modify DirHandler or write your own HttpHandler.

WEBrick alter HTTP response headers on a per-file extension basis

lib/dps/compression.rb

module Dps 
class Compression
def initialize(app)
@app = app
end

def call(env)
status, headers, response = @app.call(env)
if File.extname(env['REQUEST_URI']) == ".svgz" && status == 200
headers["Content-Encoding"] = "gzip"
else
nil
end
[status, headers, response]
end
end
end

config/application.rb

config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.middleware.insert_before("ActionDispatch::Static", "Dps::Compression")

Generate HTML page using rails without a webserver

I've found a solution:

require '/path/to/application.rb'
app = APPName::Application.initialize!
session = ActionDispatch::Integration::Session.new(app)
session.get '/'
puts session.body

The 304 Not Modified Processing Message with the Webrick Server Application - Localhost

Just clear your cache and cookies...it will be fine

How to run .erb files in browser with a customized directory?

Are you using a different Ruby framework or need to access server data? If not, you can generate HTML from erb with the command line, then simply open the HTML:

<% # page.erb %>
<html>
<head>
<title>My erb</title>
</head>
<body>
<h1>it is: <%= Time.now %></h1>
</body>
</html>

To compile and open from the command line:

$ erb page.erb > page.html
$ open page.html


Related Topics



Leave a reply



Submit