alexloh
alexloh

Reputation: 1616

How do I use JavaScript with Ruby rack?

I am not a web programmer and I am still learning about rack. I know how to work the simplest lobster example that just uses call(env) and returns a bunch of HTML.

How do I use javascript and css with this? Where do I put my .js and .css files? And for that matter where do I put my image .jpg files?

Upvotes: 0

Views: 831

Answers (2)

WarHog
WarHog

Reputation: 8710

I think that main purpose of rack is handling of HTTP requests. Rack has handlers that allow it to connect to all web application servers (WEBrick, Mongrel etc.) But handling of static types of content - js, css, pictures - is the aim of web servers. One of the Rack's purposes - is to be a bridge between servers and Ruby web frameworks (Rails, Sinatra etc.)

UPDATE

If you want to handle static file with rack's help, you can use Rack::Static class. For example, I have following folder structure: /my_rack_folder |_ config.ru |_ my_app.rb |_/media |_test

**** config.ru ****
require './my_app'
use Rack::Static, :urls => ["/media"]
run MyApp.new

**** my_app.rb ****
class MyApp
  def call(env)
    [200, {"Content-Type" => "text/html"}, ["Hello from the Rack World!"]]
  end
end

and test - static file. Now you can run rack using rackup rackup config.ru - by default it start on port 9292 with WEBrick server. If you open your browser by address localhost:9292, you will see text rendered by my_app.rb file, and by address localhost:9292/media/test - content your static file.

Upvotes: 1

Caley Woods
Caley Woods

Reputation: 4737

If you're starting to venture into JS and CSS and trying it out on Rack perhaps it's time to move to something like Sinatra.

Upvotes: 0

Related Questions