How to get Sinatra to auto-reload the file after each change?
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
.
How to get Sinatra to auto-reload the file after each change?
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
.
Rack::Reloader not picking up changes
See here for a detailed explanation of what's happening. Essentialy everytime your file is changed, Rack::Reloader
re-requires it.
Unfortunately with Sinatra, if you redefine a route a second time (which is what happens when you re-require), sinatra ignores the new definition since get '/' do end
is already defined!
What you will need to do is reset any defined routes you have as well:
# inside app.rb
require 'sinatra'
require 'rack'
configure :development do
Sinatra::Application.reset!
use Rack::Reloader
end
get '/' do
'hi'
end
Note that sometimes it takes a few seconds (5s on my machine) for the changes to be reloaded and I recommend you take a look at the alternatives here
Sinatra requires restart
The book has something on the topic now: http://sinatra-book.gittr.com/#automatic_code_reloading
Sinatra view doesn't refresh after some delete requests
There is reloading a page and there is redirecting to a page.
When you use the redirect
method of Sinatra it responds with a 302 or 303 status code.
# @see https://github.com/sinatra/sinatra/blob/v2.0.0/lib/sinatra/base.rb#L271
# Halt processing and redirect to the URI provided.
def redirect(uri, *args)
if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
status 303
else
status 302
end
# According to RFC 2616 section 14.30, "the field value consists of a
# single absolute URI"
response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
halt(*args)
end
By calling redirect "/"
you are asking the browser to redirect to "/", which would normally trigger a fresh page. However, you're already on that page, so the jQuery code is receiving a redirect and you're asking for a reload via location.reload()
upon success. Success is generally indicated by responding with a 2xx code, so I'd posit that the redirect means the success handler isn't firing, and because the browser realises you're already on the page the redirect has asked for it doesn't attempt a redirect either and you end up with no fresh data.
The route should probably look more like this:
delete '/delete/:record_id' do
id = get_record_id
delete_record id
halt 200 # think about handling errors too
end
The success handler should now fire. Moral of the story, be explicit in what you want to do, don't rely on side effects (like a redirect sharing some properties of a reload) to do what you want.
Related Topics
Typing 'Rails Console' Doesn't Start
Irb History Not Working with Ruby 2.3.0
Mocha Mock Carries to Another Test
Why Do I Get Uninitialized Constant Devise Name Error When Running Webrick Server
Running into Smtp Error When Trying to Send Email in Ror App
How to Dynamically Create a Local Variable in Ruby
Is It Possible for Rspec to Expect Change in Two Tables
How to Prevent Nokogiri from Adding <Doctype> Tags
Dynamically Set Local Variables in Ruby
Change the Binding of a Proc in Ruby
How to Debug in Rubymine 4.5 Using Ruby 1.9.3
Using Ruby Convert Numbers to Words
Project Euler 1:Find the Sum of All the Multiples of 3 or 5 Below 1000
Errno::Econnrefused: Connection Refused - Connect(2) for Action Mailer
Is There a Built-In Binary-Search in Ruby