I have one rails API application (App1), which doesn't have any database (no schema.rb file), but i am using some other application's database without adding any heroku postgres addon so when i push this application to heroku, i set the environment variable as DATABASE_URL = [ Database url of App2 ], also i have added some test cases with mini-test and enabled CI in heroku, it will run the test before the deployment but that time i am getting this error
and this is my application.rb
require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module DemoGroup
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
end
end
is there any way i can run this test in heroku
The correct way to do this is by attaching the Postgres addon from app2 to app1.
Lets say you set DATABASE_URL on app1 to point to the attachment on app2. Heroku then needs to do maintenance - which changes the database url as its shifted to another server. Heroku is nice enough to automatically update DATABASE_URL on app2 - but app1 will be pointing to an invalid url.
This assumes you have the Heroku CLI client installed.
First get a list of the attachments:
$ heroku addons
Remove the default Postgres addon
$ heroku addons:remove heroku-postgresql:dev --app app1
Replace app2 with the name of your application on heroku.
Attach the addon from the other application.
Then attach the Postgres add-on from app2 to app1:
$ heroku addons:attach attachment_name -a app1
Schema.rb
schema.rb is required for ActiveRecord to work properly. If app1 does actually create migrations you can just commit an "empty" schema.rb:
ActiveRecord::Schema.define(version: 0) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
end
Related
Rails Admin works locally for me, but when I deploy to Heroku, it fails with:
ActionView::Template::Error (The asset "rails_admin/rails_admin.css" is not present in the asset pipeline
I am on Rails 5.2.6 and Rails Admin 2.0.2
I added initializers/assets.rb with this line:
Rails.application.config.assets.precompile += ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']
but those files do not exist in assets/javascripts and assets/stylessheets.
This app was originally built as an API only app, but I've added all the missing middleware to make it work as a normal Rails app.
This is my application.rb:
require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CheckwizApi
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = false
# use uuid's as primary_key
config.active_record.primary_key = :uuid
config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
config.i18n.default_locale = :en
config.active_job.queue_adapter = :sidekiq
# Added because RailsAdmin needs them
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Flash
config.middleware.use Rack::MethodOverride
config.middleware.use ActionDispatch::Session::CookieStore
end
end
Note that I have set config.api_only = false and added back the required middleware, but to no avail. If I set it to true, same error.
If I remove the assets file I added above, same error.
The assets/javascripts and assets/stylesheets folders are empty. Is that the problem?
I have looked at several other closed issues related to this and most of them advise adding the above middleware, or clearing tmp/cache and restarting. Neither worked for me sadly.
A number of things had to be done to fix this.
Firstly, I updated my config/initializers/assets.rb file to this:
Rails.application.config.assets.enabled = true
# Prevent initializing the application before assets are precompiled (required for heroku)
Rails.application.config.assets.initialize_on_precompile = false
# Add Rails Admin assets (required)
Rails.application.config.assets.precompile += ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']
Then, by examining the Rails logs on Heroku:
heroku logs -t --remote production
I spotted this line:
Detected manifest file, assuming assets were compiled locally
So inside public/assets I deleted the manifest file (it is created if you compile assets locally, which perhaps I had done once to try to solve this issue, and forgot it was there).
This then caused the following error to appear in the heroku log:
Sprockets::ArgumentError: link_directory argument must be a directory
So I then added a hidden empty file called .keep to both the javascripts and stylesheets folders inside app/assets
Now Heroku runs the asset precompile task as part of the deploy (ie. whilst building the slug) and the rails_admin js and css files are created.
More info here and here which helped figure this out.
I have a project with a Vue frontend and a rails backend. It's just a very simple API on the backend, no database or anything. It's working fine locally but now I want to deploy it on Heroku.
However, when I run it I get the following error.
-----> Detecting rake tasks
!
! Could not detect rake tasks
! ensure you can run `$ bundle exec rake -P` against your app
! and using the production group of your Gemfile.
! rake aborted!
! URI::InvalidURIError: bad URI(is not URI?): ://user:pass#127.0.0.1/dbname
...
...
/activerecord-6.0.2.1/lib/active_record/railties/databases.rake
Based on various SO posts/Heroku documentation, I have already tried:
bundle exec rake -P RAILS_ENV=production - everything looks ok
adding the rake dependency in Gemfile
removing the sqlite dependency in Gemfile
removing the BUNDLED WITH from Gemfile.lock
But still the same error.
I guess it's related to my database config, but I don't have any database on my project so this seems like an unnecessary task anyway. I tried commenting out railties from my Gemfile but it's still there as a dependency for other gems. When I deploy after making this change, it still hits the same task and fails.
Link to repo branch
Instead of require 'rails/all' which requires all the Railties including ActiveRecord you need to explicitily require the railties you want to use:
require File.expand_path('../boot', __FILE__)
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
# require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
module Draw
class Application < Rails::Application
# You don't need this nonsense since you don't even have config/application.yml
# ENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
# config.active_record.raise_in_transactional_callbacks = true
end
end
If you don't want to use a ActiveRecord you can just get rid of /db and /config/database.yml.
You also don't need to add have gem 'rake' in your Gemfile as rails depends on it anyways.
Needed to fully remove use of ActiveRecord from the project. As Max commented, in a new app this can be done by doing rails new app_name --skip-active-record --api, to do this for an existing project see this explanation
I've built my first rails app and I'm trying to deploy it to Heroku but the app just crashed every time I push. When I try to log into the rails console, this is what is see:
Running rails console attached to terminal... up, run.6183
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:177:in
rescue in spec': Specified 'postgresql' for database adapter, but the
gem is not loaded. Addgem 'pg'` to your Gemfile (and ensure its
version is at the minimum required by ActiveRecord). (Gem::LoadError)
I'm not even using any database in my app.
This is what my database.yml file looks like:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
development:
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
production:
Can someone help me here?
Thanks!
The problem is that heroku will either rewrite database.yml or set environment variables that rails will pick up automatically.
You need to actually stop Activerecord loading:
To do this you replace require "rails/all" at the top of config/application.rb
With
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
You'll also need to remove anything in config/initializers or config/environments that references activerecord
I believe heroku uses its own automatic database configuration ignoring your database.yml which explains why it still expects postgres. I'm not sure you can do anything about it other than include the gem.
You could do this in your gem file.
group :production do
gem 'rails_12factor'
end
I am pretty much brand new to RoR and such.
I am following a video tutorial to build my own web-based app, and I got to the step:
git push heroku master
When in git bash, it was coming up with an error that claimed it couldn't compile ruby. Now, it says it is launched and deployed, but there is still the same error on the page for my app, http://infinite-mountain-6131.herokuapp.com/
Any ideas?? I can add files if needed.
Requested file(s):
app/config/application.rb from my comment
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# config/application.rb
config.assets.initialize_on_precompile = false
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Myrubyblog
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
Line 6 that was mentioned in my error is the config.assets.initialize which I put in there with line 5 as suggested to fix my problem.
This is what happens when I run the migrate as suggested (heroku run rake db:migrate)
Running 'rake db:migrate' attached to terminal... up, run.6274
rake aborted!
NameError: undefined local variable or method 'config' for main:Object
/app/config/application.rb:6:in '<top <required>>'
/app/Rakefile:4:in 'require'
/app/Rakefile:4:in '<top <required>'
<See full trace by running task with --trace>
As a rule, there are two types of error you can get when hosting a Rails app on Heroku:
Heroku Error
-
Rails Error
--
Error
The difference between the two is important - rails errors will only occur if your operating environment is actually "running" your Rails application. Heroku errors will occur if your operating environment / Heroku will not load correctly
The problem you have is definitely a Heroku issue - one which is typically created by a lack of db connectivity. The way to fix this issue is to ensure your application has all the necessities to run - most notably the correct db
You'll be best using the following:
$ heroku run rake db:migrate
However, I appreciate this won't be the only issue you'll have
Heroku Deployment
As you've said you're a "beginner" to ROR, let me give you some ideas
Firstly, when you write a question on here, it helps to divulge as much information as possible - typically from the logs, or any other specific error handling mechanism
Secondly, you want to ensure that everything required to get your application running has been achieved. Most notably, when you mention Heroku cannot compile the Ruby application, you'll need to provide information on why this is the case -- there'll probably be a gem conflict (SQLite3) or similar
Thirdly, you need to ensure you have migrated your database. This is the single biggest reason why "Heroku errors" appear - deploying your Rails app doesn't mean the migrations you made locally will persist - you need to ensure you have the db updated as you require, which can be done as follows:
$ heroku run rake db:migrate
I am building a rails 4 app that does not use any database. I have successfully disabled ActiveRecord on my development machine by following a few guides online by deleting database.yml and replacing
require 'rails/all'
with
require "action_controller/railtie"
require "action_mailer/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
It works locally but when I try to deploy it on the server running unicorn, I get this on the err logs
ERROR -- : ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
/home/rtb/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
the app worked fine on the production unicorn server when I had a database.yml on and activerecord enabled. Is there something I am missing?
The ConnectionManagement middleware from ActiveRecord is probably still active. This middleware manages the connection pool for each request. It should not be active if you haven't loaded ActiveRecord.
You can manually remove the middleware with the following line in your Rails configuration:
config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"