Nick Vanderbilt
Nick Vanderbilt

Reputation: 38450

bundler not working with rack application

Here is my code

# config.ru
require "rubygems"
require "bundler"
Bundler.setup
Bundler.require

FooApp = Proc.new do |env|
  [200, {'Content-Type' => 'text/plain'}, "foo"]
end
Routes = Rack::Mount::RouteSet.new do |set|
  set.add_route FooApp, { :request_method => 'GET', :path_info => %r{^/foo$} }, {}, :foo
end
run Routes

My Gemfile looks like this

source :rubygems
gem 'rack-mount'

I did bundle install which produced Gemfile.lock.

When I run

rackup config.ru

I get following error

uninitialized constant Rack::Mount (NameError)

The code works if I remove dependency on bundler. However I want bundler to work . This code is a small form of large application.

Upvotes: 0

Views: 904

Answers (1)

rubiii
rubiii

Reputation: 6983

Bundler.require tries to load a file called rack-mount.rb (same as the name of the gem) inside the gem's lib directory. That's a Rubygems convention. With most Rack gem's this does not work, because they don't contain such a file.

Instead it's all about namespacing. rack-mount's lib folder for example contains a rack folder which contains a file called mount.rb (see rack-mount/tree/master/lib on GitHub).

To fix the problem, you have to tell Bundler which file to require:

source :rubygems
gem "rack-mount", :require => "rack/mount"

The Bundler documentation contains further information about how to write a Gemfile.

Upvotes: 1

Related Questions