简体   繁体   English

如何在Heroku中为Sinatra应用程序设置数据库?

[英]How do I set up a database for a Sinatra app in Heroku?

I made a Sinatra application that runs with a SQLite3 database. 我制作了一个运行SQLite3数据库的Sinatra应用程序。 Everything runs smoothly on my local machine. 一切都在我的本地计算机上顺利运行。

When I am deployed it to Heroku I had to change a few things, namely a Gemfile and a Procfile, while also changing my database code to this: 将其部署到Heroku时,我必须更改一些东西,即Gemfile和Procfile,同时还要将数据库代码更改为:

#Within a class

#Before
@DB = Sequel.connect('sqlite://database.db')

#After
@DB = Sequel.connect(ENV['DATABASE_URL'] || 'postgres://localhost/db')

I changeed the database to Postgres, rather than SQLite3 because SQLite3 isn't supported with Heroku. 我将数据库更改为Postgres,而不是SQLite3,因为Heroku不支持SQLite3。 (This may be where my problem stems from.) (这可能是我的问题所在。)

Here is my Gemfile: 这是我的Gemfile:

source "https://rubygems.org"
ruby "1.9.3"

gem "sinatra"
gem "sequel"
gem "pg"
gem "postgres"
gem "json"

And my Procfile: 而我的Procfile:

web: bundle exec ruby main.rb -p $PORT

I made a git repository, and ran heroku create . 我创建了一个git存储库,并运行heroku create I added all the files, committed and pushed them using git and I got my Heroku URL and everything is fine. 我添加了所有文件,使用git提交并推送了它们,我得到了Heroku URL,一切都很好。 I ran heroku config to check my URLs and got this result: 我运行heroku config来检查我的URL并得到以下结果:

DATABASE_URL:postgres://uhyodailuzazve:hUSIuD_4MOYWmoiSGNZCPrQNyg@ec2-54-204-40-     140.compute-1.amazonaws.com:5432/d1qkn1ivl7p013HEROKU_POSTGRESQL_BLACK_URL:postgres://uhyodailuzazve:hUSIuD_4MOYWmoiSGNZCPrQNyg@ec2-54-204-40-140.compute-1.amazonaws.com:5432/d1qkn1ivl7p013

(They are the same). (他们是一样的)。 Also my stack is "cedar" 我的筹码也是“雪松”

=== enigmatic-taiga-5020 Available Stacks
bamboo-mri-1.9.2
bamboo-ree-1.8.7
* cedar

How can I have a database configured with Sinatra, going from Sequel to Postgres? 如何从Sequel到Postgres使用Sinatra配置数据库? I've looked at tutorials and they have gotten this far. 我看过教程,到现在为止。 Has anyone done this before? 有人做过吗?

@DB = Sequel.connect(ENV['DATABASE_URL'])应该可以正常工作,尽管通常您会将数据库分配给常量,而不是实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Apache with Passenger下设置Sinatra应用程序? - How do I set up a Sinatra app under Apache with Passenger? 如何在Heroku托管的Ruby / Sinatra应用中设置HTTP标头? - How do I set HTTP Headers in Ruby/Sinatra app, hosted on Heroku? 如何在heroku上的sinatra应用程序中保留和更新持久变量 - How do I keep and update a persistent variable in a sinatra app on heroku 如何在NearlyFreeSpeech上设置Sinatra或其他Ruby框架 - How do I set up Sinatra or another Ruby framework on NearlyFreeSpeech 如何使用由Sinatra驱动的Ruby应用程序将数据刮到Heroku PostgreSQL数据库中 - How do I use my Sinatra-powered Ruby application to scrape data to a Heroku PostgreSQL database 我是否需要Procfile才能将Sinatra应用程序推送到Heroku? - Do I need a Procfile to push a Sinatra app to Heroku? 如何在Heroku上托管的Sinatra应用程序中设置HTTP响应(缓存)标头 - How to set HTTP response (cache) headers in a Sinatra app hosted on Heroku 如何测试带有Memcache的Heroku上的Rack :: Cache是​​否可与Sinatra应用一起使用? - How do I test if Rack::Cache is functioning with Sinatra app on Heroku with memcache? 如何设置要在heroku上运行的应用程序? 收到sqlite3错误 - How do I set up app to run on heroku? Getting sqlite3 errors 如何将这个Ruby / Sinatra应用程序部署到Heroku? - How can I deploy this Ruby/Sinatra app to Heroku?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM