how to use "sinatra/reloader"? - ruby

I have my web.rb where I have
require 'rubygems'
require 'sinatra'
require "sinatra/reloader"
Then I start my web application by double click on the web.rb short cut. After any change in web.rb I have to exit the sinatra and run it again.
I thought that "sinatra/reloader" would help me not to manual reaload.
configuration (MS Windows)
ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]
sinatra (1.2.6, 1.0)
sinatra-advanced-routes (0.5.1)
sinatra-reloader (0.5.0)
sinatra-sugar (0.5.1, 0.5.0)
thin (1.2.7 x86-mswin32)

register Sinatra::Reloader
I changed to rack-reloader months back, realized it didn't work well even with hacks
switched back and forgot to use register
the drawback of sinatra sometimes is the slightly more wisdom-of-crowds documentation

if I start my web application from dos prompt via "ruby web.rb" everything works fine
if I start my web application
via double click web.rb or
from batch file
the application starts but sinatra-reload doesn't work
#nash thank you for your help

Related

sinatra hello world app doesn't work

I'm having trouble learning how to use Sinatra.
Versions are
Mac OSX 10.11.2
Ruby 2.2.2
Sinatra 1.4.6
I wrote the same code as the first code in this site.
# main.rb
require 'sinatra'
get '/' do
'Hello world!'
end
Then I tried a command in the same derectory
% ruby main.rb
== Sinatra (v1.4.6) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
Then I accessed http://localhost:4567 with Google Chrome, but the browser didn't recieve any data (ERR_EMPTY_RESPONSE). Also nothing logged in Terminal.
I tried other port (like 3000) by -p option, but it didn't work.
What am I doing wrong?
I updated gem and reinstalled sinatra, then it worked.
gem update --system
gem uninstall sinatra
gem install sinatra

Can't stop Sinatra using Ctrl+C when `require 'coffee-script'`

I'm running Ruby 1.9.2p180 on Ubuntu 11.04 64-bit.
I have the simplest possible CoffeeScript example using Sinatra which runs fine, but doesn't stop when I press the Ctrl+C shortcut. So every time I have to kill -9 and it's getting quite tedious.
The app.rb:
require 'sinatra'
require 'coffee-script'
get '/' do
'<script src="/app.js" type="text/javascript"></script>'
end
get '/app.js' do
coffee :app
end
The views/app.coffee:
alert 'Foo'
It works on Ruby 1.8.7 with minor modifications:
require 'rubygems'
require 'sinatra'
require 'coffee-script'
require 'json'
get '/' do
'<script src="/app.js" type="text/javascript"></script>'
end
get '/app.js' do
coffee :app
end
It also works on 1.9.2 when I remove the line require 'coffee-script', but gives me a warning:
WARN: tilt autoloading 'coffee_script' in a non thread-safe way; explicit require 'coffee_script' suggested.
Figured out that it works when using therubyracer, but fails on node. The version 0.1.97 from Ubuntu 10.10 repositories is the only exception. When using the 0.2.6 from Natty, it fails and also when installing the latest (0.4.8) using nvm, I wasn't able to install 0.1.97 via nvm.
Unable to replicate. I'm on a Mac (OS 10.6.7) running bash and Ruby 1.9.2p180. I don't get your tilt warning, either.
I also haven't experienced this problem when using The Middleman (Sinatra-based) or Rails 3.1; both use the same coffee-script gem (as does Tilt; I suspect the coffee_script is just a typo).
Have you tried updating all the pertinent gems (sinatra, coffee-script, tilt, execjs) to their latest versions? What JS environment do you have on your system (e.g. do you have node, or are you relying on therubyracer)?
On Ruby 1.9.2, Sinatra 1.3.2, CoffeeScript 2.2.0 and Node 0.6.2 this does not occur anymore.

Which HTTP web server can I use to debug ruby code using Rubymine 3.0.1?

I've being using Mongrel with Rails 2.3.X and Ruby 1.8.7 without problems, but when I switch to Ruby 1.9.2 and Rails 3, I'm having problems with mongrel to debug.
Webrick is just slow, so, do you know any alternatives?
Do you know how to fix mongrel to successfully debug rails app using rubymine 3.0.1?
RubyMine 3.1 is out now and supports a number of different servers. Under the "Run" menu, select "Edit Configurations" then go to "Defaults->Rails". There you can choose between:
WEBrick
lighttpd
mongrel
thin
trinidad
unicorn
glassfish
I couldn't get Defaults->Rails changes to affect my different configs that were set to 'Default', but I did have success changing the discrete config ('development') directly. Note: For Mongrel you need to have this in your gemfile:
gem 'mongrel', '1.2.0.pre2'
I'm using RM 3.1.1, Rails 3.0.9 and Ruby 1.9.2.

Sinatra application running on Dreamhost suddenly not working

My Sinatra application was running fine on Dreamhost until a few days ago (I'm not sure precisely when it went bad). Now when I visit my app I get this error:
can't activate rack (~> 1.1, runtime) for ["sinatra-1.1.2"], already activated rack-1.2.1 for []
I have no idea how to fix this. I've tried updating all my gems, then touching the app/tmp/restart.txt file, but still no fix.
I hadn't touched any files of my app, nor my Dreamhost account. It just busted on its own (my guess is DH changed something on their server which caused the bust).
When I originally deployed my app, I had to go through some hoops to get it working, and I seem to think I was using gems in a custom location, but I can't remember exactly where or how. I don't know my way around Rack/Passenger very well.
Here's my config.ru: (mostly grafted from around the web, I don't fully understand it)
ENV['RACK_ENV'] = 'development' if ENV['RACK_ENV'].empty?
#### Make sure my own gem path is included first
ENV['GEM_HOME'] = "#{ENV['HOME']}/.gems"
ENV['GEM_PATH'] = "#{ENV['HOME']}/.gems:"
require 'rubygems'
Gem.clear_paths ## NB! key part
require 'sinatra'
set :env, :production
disable :run
require 'MY_APP_NAME.rb'
run Sinatra::Application
You could try 'pinning' your gem versions before they are required. If you have command line access to the server try this:
gem list
This should show you which gems are installed. But you do say you have some custom gem paths which may not work for this. Something is calling a 'require "rack"' with a different version to what your application is expecting. It may be Passenger, which means the best you can hope for is to upgrade to the latest version of Sinatra.
After Gem.clear_paths, you could try this:
gem 'rack', '~>1.1'
gem 'sinatra', '~>1.0' # NB use whatever gem list shows you as the version of sinatra you were using when you deployed your application.
It's the typical gem activation problem. Use Bundler to get around it.
Looks like you hit a non supportable Sinatra, Rack, Tilt version.
Take a look here how to solve that: http://codex.heroku.com/past/2010/12/14/sinatra_on_dreamhost/

How to get Sinatra to auto-reload the file after each change?

I am using
# my_app.rb
load 'index.rb'
and start the sever like this
ruby my_app.rb
but it never reload any changes I made in index page.
Did I miss anything here?
See the Sinatra FAQ,
"How do I make my Sinatra app reload on changes?"
First off, in-process code reloading in Ruby is hard and having a
solution that works for every scenario is technically impossible.
Which is why we recommend you to do out-of-process reloading.
First you need to install rerun if you haven’t already:
$ gem install rerun
Now if you start your Sinatra app like this:
$ ruby app.rb
All you have to do for reloading is instead do this:
$ rerun 'ruby app.rb'
If you are for instance using rackup, instead do
the following:
$ rerun 'rackup'
You get the idea.
If you still want in-process reloading, check out Sinatra::Reloader.
gem install sinatra-reloader
require 'sinatra'
require 'sinatra/reloader'
Note: it will reload only sinatra handlers (and, maybe some sinatra server configuration commands), but not custom files, which you have to reload manually.
UPD after 9 years: seems like it is already possible to reload other files using also_reload, dont_reload and after_reload -- https://github.com/sinatra/sinatra/pull/1150
You can use the rerun gem.
gem install rerun
rerun 'ruby app.rb'
OR if you are using rackup
rerun 'rackup'
gem install sinatra-reloader
require 'sinatra/base'
require "sinatra/reloader"
class MyApp < Sinatra::Base
register Sinatra::Reloader
get '/' do
"Hello Testing1!"
end
end
You may want to set environment variable to development and conditionally load the gem.
When you run the application with Passenger Standalone, just create a tmp/always_restart file:
$ touch tmp/always_restart.txt
See Passenger documentation for more info.
I like the Shotgun gem. If you're using a modular Sinatra app and have a config.ru file it's easy to run.
shotgun config.ru
Check the gem out here. It's fairly straight forward and no configuration needed.
On Windows, I am using my restart gem for this:
restart ruby my_app.rb
or, with rackup:
restart rackup
See here for more info, hope you find it useful.
You could use guard-rack. Lifted from an article at dblock.org:
Add this to your Gemfile:
group :development do
gem "guard"
gem "guard-bundler"
gem "guard-rack"
end
Then, create a Guardfile at the root of your project with this content:
guard 'bundler' do
watch('Gemfile')
end
guard 'rack' do
watch('Gemfile.lock')
watch(%r{^(config|app|api)/.*})
end
Lastly, run Guard, like so: bundle exec guard, and rackup will reload every time.
If you only change your templates sinatra will always rerender them if you set your environment to development:
ruby app.rb -e development

Resources