Getting a 404 on Ruby Sinatra App - Heroku - ruby

I have a simple Sinatra App. Works fine on local machine.
Deployed it to Heroku and I keep getting a 404 on the logs.
I promise I searched the web for around 5h straight and can't figure this out.
The logs I get are:
2014-11-08T00:17:12.537150+00:00 heroku[web.1]: Starting process with command `bundle exec rackup config.ru -p 51609`
2014-11-08T00:17:12.840926+00:00 heroku[web.1]: Process exited with status 143
2014-11-08T00:17:16.370395+00:00 app[web.1]: [2014-11-08 00:17:16] INFO WEBrick 1.3.1
2014-11-08T00:17:16.370515+00:00 app[web.1]: [2014-11-08 00:17:16] INFO ruby 2.0.0 (2014-10-27) [x86_64-linux]
2014-11-08T00:17:16.371280+00:00 app[web.1]: [2014-11-08 00:17:16] INFO WEBrick::HTTPServer#start: pid=2 port=51609
2014-11-08T00:17:16.745163+00:00 heroku[web.1]: State changed from starting to up
2014-11-08T00:17:35.351527+00:00 heroku[router]: at=info method=GET path="/" host=pacific-refuge-6392.herokuapp.com request_id=406b2ea5-5898-4a86-8a26-6bf71f70d3f6 fwd="200.170.116.105" dyno=web.1 connect=3ms service=22ms status=404 bytes=319
2014-11-08T00:17:35.352064+00:00 app[web.1]: 200.170.116.105 - - [08/Nov/2014 00:17:35] "GET / HTTP/1.1" 404 18 0.0030
My Gemfile is like this:
ruby '2.0.0'
source "https://rubygems.org"
gem 'sinatra-base'
gem 'sinatra-assetpack'
gem 'sinatra-asset-pipeline'
gem 'sass'
gem 'datamapper'
gem 'pony'
group :production do
gem "pg"
gem "dm-postgres-adapter"
end
group :development, :test do
gem "sqlite3"
gem "dm-sqlite-adapter"
end
Proclife:
web: bundle exec rackup config.ru -p $PORT
I tried it with ruby instead of rackup and a few other variations I found.
Config.ru
require './my_launch'
run Sinatra::Application
And finally, my app: my_launch.rb:
require 'sinatra/base'
require 'sinatra/assetpack'
require 'sass'
require 'data_mapper'
require 'dm-migrations'
require 'pony'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/mylaunch.db")
class Users
include DataMapper::Resource
property :id, Serial
property :email, String, :required => true, :format => :email_address
property :created_at, DateTime
end
DataMapper.finalize.auto_upgrade!
class MyLaunch < Sinatra::Base
set :sessions, true
register Sinatra::AssetPack
assets do
css :main, [
'/css/*.css'
]
css_compression :sass
end
get '/' do
erb :index
end
post '/' do
n = Users.new
n.email = params[:email]
n.created_at = Time.now
n.save
redirect '/obrigado'
end
get '/obrigado' do
erb :obrigado
end
get '/admin' do
#users = Users.all :order => :id.desc
erb :admin
end
run! if app_file == $0
end
My database seems OK.
To test that I fired up heroku console and created an entry on my database.
Via requireing my_launch.rb and making a Users.new and later, Users.save
https://github.com/abarro/mylaunch

In your config file your calling run Sinatra::Application which differs from the your Sinatra inherited class where all your routes are defined. Change it to
require './my_launch'
run MyLaunch

Related

sinatra activerecord doesn't find model on heroku

I have a classic style sinatra app (not modular), using sinatra-activerecord.
This is my config.ru
require 'rubygems'
require 'bundler'
Bundler.require
require './config/environment'
require './babycomptes' # main application file
run Sinatra::Application
and config/environment.rb:
APP_ENV = ENV["RACK_ENV"] || "development"
ENV['SINATRA_ENV'] ||= "development"
require 'require_all'
require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
require_all 'app'
The main app file (babycomptes.rb) is sitting at the root of my project. It contains:
require 'sinatra'
require 'sinatra/namespace'
require 'sinatra/activerecord'
require 'bcrypt'
require 'securerandom'
require_relative './app/helpers/helpers.rb'
# Dir["./app/models*.rb"].each {|file| require file }
get '/signup' do
erb :'signup/new'
end
post '/signup' do
#user = User.new(params[:user]) # this is line 51
# cut for brievity
end
The app works fine when I rackup locally and when I run heroku local, but runs into an error on heroku :
2020-12-30T06:20:27.936954+00:00 app[web.1]: 2020-12-30 06:20:27 - NameError - uninitialized constant User:
2020-12-30T06:20:27.936967+00:00 app[web.1]: /app/babycomptes.rb:51:in `block in <top (required)>'
no clue where to start looking....

uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement

Im currently working on a sinatra app, and im having a trouble regarding postgresql connection to sinatra, im try to execute this command:
rake db:create
to create the database but it throws this error.
C:\Users\John\Documents\Registration_Sinatra>rake db:create
rake aborted!
NameError: uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
LoadError: cannot load such file -- sinatra/activerecord
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
(See full trace by running task with --trace)
this is my app.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'pg'
require './config/environments'
class RegistrationSinatra < ActiveRecord::Base
end
get '/' do
erb :index
end
this is my environments.rb
configure :development do
#DEFAULT_CONN = {database: 'development_registration_sinatra', user: 'postgres', password: 'secret123', host: 'localhost'}
db = URI.parse(ENV['DATABASE_URL'] || "postgres://#{#DEFAULT_CONN[:host]}/#{#DEFAULT_CONN[:database]}?user=#{#DEFAULT_CONN[:user]}")
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => #DEFAULT_CONN[:host],
:username => #DEFAULT_CONN[:user],
:password => #DEFAULT_CONN[:password],
:database => db.path[1..-1],
:encoding => 'utf8')
end
this is my gemfile
source 'https://rubygems.org'
ruby "2.2.2"
gem 'sinatra'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'tux'
gem 'pg'
and my Rakefile
require './app/app'
require 'sinatra/activerecord/rake'
hope you guys can pin point what's wrong with my sample app so i can progress thanks.
Here is the solution: https://github.com/janko-m/sinatra-activerecord/pull/66
In your Gemfile, add:
gem "activerecord", "< 5.0.0"
run bundle update and it will work.

Sinatra with Postgres and ActiveRecord | Error Pushing to Heroku is: Invalid DATABASE_URL

I have an application written in Sinatra that works locally but that I cannot seem to push up to heroku.
When I attempt to push to heroku I get the following error:
-----> Writing config/database.yml to read from DATABASE_URL
Could not detect rake tasks
ensure you can run $ bundle exec rake -P against your app with no environment variables present
and using the production group of your Gemfile.
This may be intentional, if you expected rake tasks to be run
cancel the build (CTRL+C) and fix the error then commit the fix:
rake aborted!
Invalid DATABASE_URL
My Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
gem 'sinatra', require: 'sinatra/base'
gem "pg"
gem "activerecord"
gem "sinatra-activerecord"
gem "rake"
gem 'minitest', require: false
gem 'rack-test', require: false
gem 'faraday'
gem 'json'
gem 'minitest-reporters'
My database.yml file
development:
adapter: postgresql
encoding: unicode
database: calendar_development
host: localhost
password:
test:
adapter: postgresql
encoding: unicode
database: calendar_test
pool: 5
host: localhost
password:
My config.ru
require './app'
run Sinatra::Application
ENV['RACK_ENV'] ||= 'development'
Procfile
web: bundle exec rackup config.ru -p $PORT
Rakefile
require "sinatra/activerecord/rake"
require 'rake/testtask'
require "./app"
task :default => :test
env = ENV["SINATRA_ENV"] || "development"
Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList["test/**/*_test.rb"]
t.verbose = false
end
The app.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'json'
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file }
env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index
env = env_arg || ENV["SINATRA_ENV"] || "development"
use ActiveRecord::ConnectionAdapters::ConnectionManagement # close connection to the DDBB properly...https://github.com/puma/puma/issues/59
databases = YAML.load_file("config/database.yml")
ActiveRecord::Base.establish_connection(databases[env])
if env == 'test'
User.destroy_all
end
I removed all the environment test from my app.rb file and moved it to environments:
app.rb
require './config/environments'
Then I changed the DB setup - first of all, removed all the ENV[SINATRA_ENV]
environments.rb
configure :production, :development, :test do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/calendar_development')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
I changed my Rakefile
namespace :db do
task :seed do
seed_file = File.join('db/seeds.rb')
load(seed_file) if File.exist?(seed_file)
end
end
I followed sybohy's advice and in my config.ru file, I removed the last line - leaving
config.ru
require './app'
run Sinatra::Application
I ran:
heroku addons | grep POSTGRES
grabbed the name of the db (HEROKU_POSTGRESQL_JADE)
heroku pg:reset HEROKU_POSTGRESQL_JADE
and then migrated
heroku run rake db:migrate
So far this seems to have fixed the errors! I will update if I find more!

Mongoid error in heroku: Database should be a Mongo::DB, not a nil class

I have a Sinatra app on heroku and it keeps crashing due to this error:
app/vendor/bundle/ruby/1.9.1/gems/mongoid-1.2.14/lib/mongoid/config.rb:52 in 'master': Database should be a Mongo::DB, not a nil class
I set up Mongoid 3.x according to the heroku instructions, and the app works on my local machine, so I'm not sure what's causing this problem. My gemfile looks like this:
source "https://rubygems.org"
ruby "1.9.3"
gem 'sinatra'
gem 'mongo'
gem 'mongoid'
gem 'bson_ext'
gem 'json'
gem 'nokogiri'
gem 'aws-s3', '0.6.2', :require => 'aws/s3'
gem 'sinatra-reloader'
gem 'debugger'
gem 'thin'
Here's my mongoid.yml:
development:
sessions:
default:
database: db
hosts:
- localhost:27017
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: true
And here's my app file:
require 'bundler/setup'
require 'sinatra'
require 'json'
require 'mongo'
require 'mongoid'
Mongoid.load!('mongoid.yml', :production)
def get_connection
return #db_connection if #db_connection
db = URI.parse(ENV['MONGOHQ_URL'])
db_name = db.path.gsub(/^\//, '')
#db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
#db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
#db_connection
end
db = get_connection
class Model1
include Mongoid::Document
field :name, :type => String
end
I shouldn't have to specify a database name since I'm using the uri field, so I'm not sure why the database if nil?

problem with task rake, ruby

I have got a task in rake that run my server sinatra , it doesn't work , the same script in ruby works. Why ?? can I run server sinatra in rake task??
task :server do
begin
require 'rubygems'
require 'sinatra'
rescue LoadError
p "first install sinatra using:"
p "gem install sinatra"
exit 1
end
get '/:file_name' do |file_name|
File.read(File.join('public', file_name))
end
exit 0
end
Create a class that is inherited from a Sinatra::Base class
#app.rb
require 'sinatra'
class TestApp < Sinatra::Base
get '/' do
"Test"
end
end
And then run your application from rake:
#Rakefile
$:.unshift File.join(File.dirname(__FILE__), ".")
require 'rake'
require 'app'
task :server do
TestApp.run!
end

Resources