I'm building a basic Ruby app with Sinatra and SendGrid.
When I try and run the file ruby app.rb the site does not load. When I remove require 'sendgrid-ruby' the app loads. Code below.
app.rb
require "sendgrid-ruby"
require "sinatra"
set :app_file, __FILE__
get '/' do
puts "hi"
end
Gemfile
gem 'sinatra'
gem 'sendgrid-ruby'
Any help? I don't have a config.ru or anything else at this point. No error logging. It just hangs at waiting for LocalHost.
== Sinatra (v1.4.7) 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
Ruby version: ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15]
EDIT:
Previously, I had require "sinatra" first and I didn't have set :app_file, __FILE__ in there at all.
Related
I trying to create a fairly minimal example for templates and teaching.
I create my app.rb file
require 'sinatra'
get '/' do
"Minimal!__ !_!"
end
My Gemfile just has
source 'https://rubygems.org'
gem 'rspec'
gem 'thin'
I started up Sinatra
$ ruby app.rb
== Sinatra (v2.0.5) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
and I can visit the page
but when I then change the code the page is cached and the new content doesn't show unless i stop and start the server.
I've read the Sinatra documentation but still can't figure it out.
I've tried adding
set :sessions, false
and
cache_control :off
to no avail
If you were having problems with the cache you could tell the browser not to cache anything:
cache_control :no_cache
You might also add Pragma and Expires to the header:
headers \
"Pragma" => "no-cache",
"Expires" => "0"
and put it all in a before filter:
before do
cache_control :no_cache
headers \
"Pragma" => "no-cache",
"Expires" => "0"
end
Or, since you're doing demonstrations, open the browser's inspector and turn off caching. Both Chrome and Firefox have this option.
(OP Adding this) A minimalist version for one call might be just to have
headers "Expires" => "0"
within the get in question
You need to use sinatra-reloader
Based on the example code, your app seems to be of the "classic" type.
Therefore simply load the reloader library into your app:
require 'sinatra'
require 'sinatra/reloader' if development?
get '/' do
"Minimal!__ !_!"
end
Since you're using a Gemfile, ensure that you've the sinatra-contrib gem listed as well (and install it via bundle install):
source 'https://rubygems.org'
gem 'rspec'
gem 'thin'
gem 'sinatra', '~> 2.0`
gem 'sinatra-contrib', '~> 2.0'
Otherwise install the gem directly: gem install sinatra-contrib
Now onwards anytime you start up the server in 'development' mode and make changes to either your app.rb or your views/index.erb, the changes will be reflected in your browser with a refresh.
I've been working on a web application in Ruby using Sinatra. While in development, I really never had a lot of issues with sessions. However, now i'm ussing passenger to actually deploy the application I have quite a lot of issues regarding that session data keeps getting 'reset'.
I've seen other stack overflow questions related to this problem but no answer has yet fixed it for me. I've tried a couple of things:
Putting 'use Rack::Session::Pool' inside config.ru as suggested here
Used Rack::Session::Pool like explained here
Tried Memcache as explained here
Tried Redis-Rack (gives me an error: Exception NoMethodError in Rack application object (undefined method `foreign_key' for nil:NilClass))
Nothing seems to be helping really... I either end up having an error or my session gets reset each request. I know that passenger probably uses multiple threads and that that is the cause for the sessions to not be working, but I cannot seem to find a solution to the problem.
Am I missing something obvious here? Any suggestions?
Gemfile
source "https://rubygems.org"
gem 'mongo', '1.8.6'
gem 'sinatra', '1.4.8'
gem 'mongo_mapper'
gem 'bson_ext'
gem 'active_model_serializers'
gem 'activemodel-serializers-xml'
gem 'sinatra-flash'
gem 'sinatra-param', require: 'sinatra/param'
gem 'rack-recaptcha2', git: 'https://github.com/nicolas-simplex/rack- recaptcha'
gem 'mail'
gem 'slugify'
gem 'biz'
gem 'bcrypt'
gem 'redis-rack'
group :development do
gem 'mailcatcher', '~> 0.6.4'
end
config.ru
It had different forms depending on if I used just Rack::Session::Pool, Memcache or Redis. This one was the one I used for redis:
require 'rubygems'
require 'sinatra'
require File.expand_path '../app.rb', __FILE__
require 'rack'
require 'rack/session/redis'
require_relative './app'
app = App.new
sessioned = use Rack::Session::Redis.new(app)
run sessioned
App.rb
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'slugify'
class App < Sinatra::Base
use Rack::Protection
set :bind, '0.0.0.0'
end
require_relative './config/init' # Initialize configuration files
require_relative './helpers/init' # Initialize helpers
require_relative './routes/init' # Initialize routes
require_relative './models/init' # Initialize models
require_relative './util/tokens' # Token utility
... # Some database seeding, basic setup for some data I use
TL;DR
Sinatra and Passenger together resets sessions in production, while this doesn't happen in development.
After some more researching I found the configuration option passenger_sticky_sessions. Because in my web application session storage is quite important, this works fine for me. However, the use of this is not recommended in all use cases because all the clients sends will be routed to the same originating application process.
Documentation: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_sticky_sessions
With this configuration parameter 'on' in my conf file in nginx/sites-enabled, I was able to just use the simple rack session pool:
use Rack::Session::Pool, path: '/', expire_after: 2592000
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
im developing a application in ruby with sinatra. evrything worked finely until i put it on heroku. heroku gives me internal server error but no error code ):
currently my workstation is a windows computer.
my log loooks like this: http://i.imgur.com/Xd3QAms.png
config.ru
require 'tilt/haml'
require 'sass/plugin/rack'
require '4c96748'
run Sinatra::Application
gemfile
source 'https://rubygems.org'
ruby '2.2.3'
gem 'sinatra', '1.1.0'
procfile
web: bundle exec rackup config.ru -p $PORT
4c96748.rb
require 'sinatra'
require 'tilt/haml'
get '/' do
haml :index
end
pleaase help me, what do i need to do?
try following in your 6c96748.rb
require 'rubygems'
require 'sinatra'
require 'haml'
get '/' do
haml :index
end
From your logfile:
LoadError - cannot load such file -- haml
There is no Haml installed on Heroku. Every dependency you need on Heroku needs to be in your Gemfile.
Add the following Line to Gemfile:
gem 'haml'
Don't forget to run bundle before commiting your changes and pushing to heroku again.
(As a sidenote, your Sinatra version is quite outdated. The current version is 1.4.6 (see https://rubygems.org/gems/sinatra))
I'm wondering how one can use Bundler with Sinatra. The idea is to use the gems that Bundler downloads inside the .gems folder.
Inside your Sinatra app, you just have to require the bundler setup:
require "bundler/setup"
require "sinatra"
get "/" do
"Hello world!"
end
Alternatively, if you don't want to add the additional require "bundler/setup" at the top of your app, you can instead invoke sinatra via bundle exec (e.g. bundle exec ruby myapp.rb)
This assumes that you have a Gemfile in the root of your application. It might look like this:
source "http://rubygems.org"
gem "sinatra"
This also assumes that you've already installed bundler (gem install bundler) and that you ran bundle install to install all the gem dependencies.
I believe the best way is described here on EngineYard blog:
# This makes sure the bundled gems are in our $LOAD_PATH
require File.expand_path(File.join(File.dirname(__FILE__), 'vendor', 'gems', 'environment'))
# This actually requires the bundled gems
Bundler.require_env
class MyApp < Sinatra::Base
# stuff
end
As my original answer was quite old but there seems to be still attention to this topic here's the latest version of bundler/sinatra setup which will cover most of the use case:
A minimal config.ru
require './my_sinatra_app'
run MySinatraApp
An environment env.rb file that requires all the bundled gems (also supports loading the current environment's group):
require 'bundler/setup'
APP_ENV = ENV["RACK_ENV"] || "development"
Bundler.require :default, APP_ENV.to_sym
Then your app file (requiring the environment) with your sinatra app (Sinatra::Base):
require_relative 'env'
class MyApp < Sinatra::Base
get "/" do
"hello world"
end
end
Start your development server with rackup, and Sinatra will be loaded via Bundler, your app will be accessible from http://localhost:9292.
$ rackup
or bundle exec rackup if needed
Make sure you have a Gemfile like the following one and you run the bundle command before starting the app
source "https://rubygems.org"
gem "sinatra"
gem "puma" # a better rack server than the default webrick
+1 for the guide on the bundler website, but if you have a simple app and use Sinatra's dsl at the top level, then you need to do the following:
in your Gemfile (tell bundler not require sinatra):
gem 'sinatra', :require => false
and in the app's file (explicitly require sinatra):
require 'rubygems'
require 'bundler'
Bundler.require
require 'sinatra'
get '/' do
'hello world'
end
To use bundler with a Sinatra application, you only need to do two things. First, create a Gemfile.
gem 'sinatra'
Then, set up your config.ru file to load the bundle before it loads your Sinatra app.
require 'rubygems'
require 'bundler'
Bundler.require
require './my_sinatra_app'
run MySinatraApp
Start your development server with rackup, and Sinatra will be loaded via Bundler.
rackup
source bundler docs