Starting WEBrick from a ruby script

Depending on how WEBrick in invoked I get different results. If I
invoke
it like “expenses/script/server” and point the browser to <http://
localhost:3000/expenses> I’ll get:

Not Found
`/expenses’ not found.
WEBrick/1.3.1 (Ruby/1.8.6/2007-09-24) at localhost:3000

However, if I change to the expenses directory and start WEBrick with
“script/server” I get a different result of:

Routing Error

no route found to match “/expenses” with {:method=>:get}

1.) why the discrepancy?
2.) from a ruby script (start_webrick.rb perhaps), how would WEBrick be
invoked? Assuming that start_webrick.rb is outside of the “expenses”
directory created by rails.

Here’s output from WEBrick, if that’s at all useful:

thufir@arrakis ~/rubyCode $
thufir@arrakis ~/rubyCode $ expenses/script/server
=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-11-25 21:01:28] INFO WEBrick 1.3.1
[2007-11-25 21:01:28] INFO ruby 1.8.6 (2007-09-24) [i686-linux]
[2007-11-25 21:01:28] INFO WEBrick::HTTPServer#start: pid=9164
port=3000
#<ArgumentError: Anonymous modules have no name to be referenced by>
[“/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/
dependencies.rb:404:in to_constant_name'", "/usr/lib/ruby/gems/1.8/gems/ activesupport-1.4.4/lib/active_support/dependencies.rb:214:in qualified_name_for’”, “/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/
lib/active_support/dependencies.rb:476:in const_missing'", "/usr/lib/ ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb:116:in reset_after_dispatch’”, “/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/
dispatcher.rb:50:in dispatch'", "/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/ lib/webrick_server.rb:113:in handle_dispatch’”,
“/usr/lib/ruby/gems/1.8/
gems/rails-1.2.5/lib/webrick_server.rb:79:in service'", "/usr/lib/ ruby/1.8/webrick/httpserver.rb:104:in service’”, “/usr/lib/ruby/1.8/
webrick/httpserver.rb:65:in run'", "/usr/lib/ruby/1.8/webrick/ server.rb:173:in start_thread’”, “/usr/lib/ruby/1.8/webrick/
server.rb:162:in start'", "/usr/lib/ruby/1.8/webrick/server.rb:162:in start_thread’”, “/usr/lib/ruby/1.8/webrick/server.rb:95:in start'", "/ usr/lib/ruby/1.8/webrick/server.rb:92:in each’”, “/usr/lib/ruby/1.8/
webrick/server.rb:92:in start'", "/usr/lib/ruby/1.8/webrick/ server.rb:23:in start’”, “/usr/lib/ruby/1.8/webrick/server.rb:82:in
start'", "/usr/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/ webrick_server.rb:63:in dispatch’”, “/usr/lib/ruby/gems/1.8/gems/
rails-1.2.5/lib/commands/servers/webrick.rb:59”, “/usr/lib/ruby/
site_ruby/1.8/rubygems/custom_require.rb:27:in gem_original_require'", "/ usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require’”,
“/
usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/
dependencies.rb:495:in require'", "/usr/lib/ruby/gems/1.8/gems/ activesupport-1.4.4/lib/active_support/dependencies.rb:342:in new_constants_in’”,
“/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
active_support/dependencies.rb:495:in require'", "/usr/lib/ruby/gems/1.8/ gems/rails-1.2.5/lib/commands/server.rb:39", "/usr/lib/ruby/site_ruby/1.8/ rubygems/custom_require.rb:27:in gem_original_require’”,
"/usr/lib/ruby/
site_ruby/1.8/rubygems/custom_require.rb:27:in require'", "expenses/ script/server:3"] [2007-11-25 21:01:36] ERROR /expenses’ not found.
127.0.0.1 - - [25/Nov/2007:21:01:36 PST] “GET /expenses HTTP/1.1” 404
278
http://developer.apple.com/tools/rubyonrails.html → /expenses
[2007-11-25 21:01:46] INFO going to shutdown …
[2007-11-25 21:01:46] INFO WEBrick::HTTPServer#start done.
thufir@arrakis ~/rubyCode $
thufir@arrakis ~/rubyCode $
thufir@arrakis ~/rubyCode $ cd expenses/
thufir@arrakis ~/rubyCode/expenses $
thufir@arrakis ~/rubyCode/expenses $ script/server
=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-11-25 21:02:05] INFO WEBrick 1.3.1
[2007-11-25 21:02:05] INFO ruby 1.8.6 (2007-09-24) [i686-linux]
[2007-11-25 21:02:05] INFO WEBrick::HTTPServer#start: pid=9167
port=3000
127.0.0.1 - - [25/Nov/2007:21:02:11 PST] “GET /expenses HTTP/1.1” 404
622
http://developer.apple.com/tools/rubyonrails.html → /expenses
[2007-11-25 21:02:25] INFO going to shutdown …
[2007-11-25 21:02:25] INFO WEBrick::HTTPServer#start done.
thufir@arrakis ~/rubyCode/expenses $
thufir@arrakis ~/rubyCode/expenses $

thanks,

Thufir

On Mon, 26 Nov 2007 05:11:41 +0000, Thufir wrote:

2.) from a ruby script (start_webrick.rb perhaps), how would WEBrick be
invoked? Assuming that start_webrick.rb is outside of the “expenses”
directory created by rails.

thufir@arrakis ~/rubyCode $
thufir@arrakis ~/rubyCode $ cat expenses.rb
system(“/home/thufir/rubyCode/expenses/script/server”)
thufir@arrakis ~/rubyCode $

This gives the result of:

Routing Error

no route found to match “/expenses” with {:method=>:get}

from http://localhost:3000/expenses which, while not being the desired
result, is at least consistent :slight_smile:

-Thufir

It’s no doubt a bit kludgy, but I think I accomplished my purpose with:

thufir@arrakis ~/rubyCode $
thufir@arrakis ~/rubyCode $ cat railsExpenses.rb
require ‘fileutils’

FileUtils.rmtree ‘expenses’

system(“rails expenses”)

configure the database

FileUtils.rm ‘expenses/config/database.yml’, :force => true

database_yml = File.open(‘expenses/config/database.yml’, ‘w’)

database_yml.puts "
development:
adapter: mysql
database: expenses_development
username: root
password:
host: localhost
socket: /var/run/mysqld/mysqld.sock
test:
adapter: mysql
database: expenses_test
username: root
password:
host: localhost
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql
database: expenses_production
username: root
password:
host: localhost
socket: /var/run/mysqld/mysqld.sock"

database_yml.close

migration

system(“expenses/script/generate migration accounts”)

FileUtils.rm ‘expenses/db/migrate/001_accounts.rb’, :force => true

_001_accounts = File.open(‘expenses/db/migrate/001_accounts.rb’, ‘w’)

_001_accounts.puts "
class Accounts < ActiveRecord::Migration

    def self.up
            create_table :accounts do |table|
                    table.column :name, :string
                    table.column :budget, :float
            end
    end

    def self.down
            drop_table :accounts
    end

end"

_001_accounts.close

rake

FileUtils.cd(‘expenses’, :verbose => true) # chdir and report it
system(“rake db:migrate --trace”)

http://localhost:3000/expenses

system(“script/server”)
thufir@arrakis ~/rubyCode $

-Thufir