I'm having trouble learning how to use Sinatra. I finally got phusion-passenger installed and working with my apache2 on Ubuntu. I have the following directories and files
/var/www/html
/var/www/html/public
/var/www/html/tmp
/var/www/html/config.ru # this is a file
The contents of /var/www/html/config.ru is copied from https://www.phusionpassenger.com/documentation/Users%20guide%20Apache.html#_tutorial_example_writing_and_deploying_a_hello_world_rack_application.
When I start up this application, I get hello world, which is great.
So next, I want to build a Sinatra app. I went ahead and created the file
/var/www/html/myapp.rb
With the contents described by http://www.sinatrarb.com/intro.html . I also did a gem install sinatra. I restarted apache. Then I went to http://localhost/ but I still see hello world of my config.ru. So then I overwrote the contents of config.ru with myapp.rb. I restarted apache. But now I get an error message
missing run or map statement (RuntimeError)
/usr/lib/ruby/vendor_ruby/rack/builder.rb:133:in `to_app'
config.ru:1:in `<main>'
/usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `eval'
/usr/share/passenger/helper-scripts/rack-preloader.rb:112:in `preload_app'
/usr/share/passenger/helper-scripts/rack-preloader.rb:158:in `<module:App>'
/usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
/usr/share/passenger/helper-scripts/rack-preloader.rb:28:in `<main>'
What am I doing wrong? How do I build a hello world Sinatra app?
Your config.ru should require and run your app, like:
require './myapp.rb'
run Sinatra::Application
And in turn, myapp should require sinatra:
require 'sinatra'
get '/' do
'Hello world!'
end
Read on about using a config.ru: http://www.sinatrarb.com/intro.html#Using%20a%20Classic%20Style%20Application%20with%20a%20config.ru
Related
I have a sinatra app with a Rakefile.rake and server.rb file. In the server.rb file I have
get '/' do
rake :test
end
the app loads up but crashes when I load localhost:4567/ and says undefined method 'rake' for sinatra application. I try and require the file by using
require '/home/user/project/Rakefile'
and I also try Rakefile.rake but both give me an error that reads "require cannot load such file." I'm using Ubuntu.
I'm not sure why sinatra can't load the rakefile, but when I run rake test in terminal that works.
Any help would be great.
get '/' do
system 'rake test'
end
I am trying to create an empty Sinatra application with a config.ru file. Right now the only way I know how is to initialise it with cucumber but doing that creates extra files which I don't need including step definitions etc. When I run 'gem install Sinatra' in my working directory it says:
Successfully installed sinatra-1.4.5
Parsing documentation for sinatra-1.4.5
Done installing documentation for sinatra after 0 seconds
1 gem installed
Yet there is no config.ru file.
How do I initialise a Sinatra app so I have just the config.ru?
From the sinatra documentation
first create a file called app.rb in your working directory containing the following code:
require 'sinatra'
get '/' do
'Hello world!'
end
now create a file called config.ru with the following content:
require './app'
run Sinatra::Application
now run
rackup -p4567
to run the application
I am attempting to run a test against a hello method contained within the hello file:
ruby hello_spec.rb
which returns:
/usr/local/Cellar/ruby/2.1.2_2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- hello (LoadError)
from /usr/local/Cellar/ruby/2.1.2_2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from hello_spec.rb:116:in `<main>'
The files are contained within the same directory. I've installed RSpec and (I believe) the necessary gems. Other people seem to have similar problems but none of the solutions have worked for me.
I am running Ruby 2.1.2
I am new to Ruby and am struggling (obviously) to get the environment properly configured. Any help is much appreciated.
Note: I didn't write any of the test code. I've literally only made the hello.rb file.
Change require 'hello' to require_relative 'hello' in your hello_spec.rb. Current directory is not included in default ruby load path by default.
Or, alternatively, add the current directory to ruby load path:
$:.unshift File.dirname(__FILE__)
Hope it helps.
I have modular style sinatra app, with the following line near the end, so that it can be run standalone:
# ... all code before this omitted
run! if __FILE__ == $0
end
# This is the end of the file
When I run this app with ruby app.rb it works fine, and webrick starts up.
However, if I run it instead with bundle exec ruby app.rb I get this error:
>bundle exec ruby app.rb
C:/Ruby200/lib/ruby/gems/2.0.0/gems/sinatra-1.4.4/lib/sinatra/base.rb:1488:in `start_server': undefined method `run' for HTTP:Module (NoMethodError)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/sinatra-1.4.4/lib/sinatra/base.rb:1426:in `run!'
What's causing this error?
Explicitly set your webserver, e.g.
set :server, 'thin'
and make sure you add whatever server you’re using to your Gemfile, e.g.
gem 'thin'
Well, I'm trying to run a simple web server through "rack". So this is my program:
require 'rubygems'
require 'rack'
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, ["Hello Rack!"]]
end
end
Rack::Handler::Mongrel.run HelloWorld.new, :Port => 9292
If I run it in the console, it works fine. If I run it in Eclipse, it ends up with error:
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
from /Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/MY_SUPER_SECRET_USER/Sites/service/service.rb:2:in `<main>'
The one that is working is called like this:
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ which ruby
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ ruby service.rb
Then when I try to open localhost:9292 it shows the expected "Hello Rack" code.
I'm on Mac OS X 10.8, with ruby 1.9.3 installed through rvm (this must be obvious). My "rack" package has been installed with sudo gem install rack
So as you can see, the Eclipse is configured with the very same ruby executable. Any suggestions will be of great help!
The line
custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
means it can't find where you installed the rack gem. Reading around the internet, I see again and again using sudo doesn't seem to work. Try just installing it and see if that fixes it.
$ gem install rack (no sudo)
I ran into this same problem, trying to require gems in Eclipse which it wasn't recognizing, even though everything seemed to be configured correctly (the gems were installed, Eclipse was pointing at the right Ruby interpreter, etc).
I ended up making it work by adding GEM_HOME and GEM_PATH variables to the Environment tab of Debug/Run Configurations.
More detailed answer here: https://stackoverflow.com/a/28419300/525338