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.rbconfig.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
Outputting Stdout to a File and Back Again
Ruby: Converting a Nested Ruby Hash to an Un-Nested One
Browsing Ruby Code a La Smalltalk
Truncate String When It Is Too Long
Get Route for Base Class of Sti Class in Rails
How to Share Code Across Chef Cookbooks in a Chef-Repo
How to Display Image Pointed by Url in Rails
Pg.Rb Segmentation Fault [Mojave Upgrade]
Setting Up Configuration Settings When Writing a Gem
Rails Parameters from Get/Post
Sorting a Multidimensional Array in Ruby
Rails - Understanding Application.Js and Application.CSS
Automating Ssh to Windows with Ruby
How to Deploy a Threadsafe Asynchronous Rails App
Capybara & Cucumber | Getting Cookies
Asynchronously Iterating Over The Response of a Request Using Thin and Sinatra