Cannot find rails_admin.css in Rail 5.4.2 - ruby-on-rails

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.

Related

Heroku Deploy: URI::InvalidURIError: bad URI(is not URI?): ://user:pass#127.0.0.1/dbname

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

Run ruby test in heroku application without database

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

Serving Compressed Assets in Heroku with Rack-Zippy

I followed this tutorial on how to compress assets in Heroku.
http://www.cheynewallace.com/serving-compressed-assets-with-heroku-rack-zippy/
Here is my Application.rb file
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module Blog
class Application < Rails::Application
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
config.exceptions_app = self.routes
config.cache_store = :memory_store
Rails.application.config.middleware.swap(ActionDispatch::Static,
Rack::Zippy::AssetServer, Rails.public_path)
end
end
My Gem File
gem 'yui-compressor', '~> 0.12.0'
gem 'rack-zippy'
I ran heroku run rake middleware and got eh following output
use Rack::Sendfile
use Rack::Zippy:AssetServer
Now when I go to Developer Tools and the Network Tab, I see that the size is nearly identical to the content for every asset. Which means the assets were not compressed.
The only similar question I could find was this one
rack-zippy and option -d in production env doesn't serve static assets
And the answer didn't work.
Should I use another Compressor, because it looks like Rack Zippy does not work.
I had a similar problem. Things started to work when I set config.assets.compile to false on config/environments/production.rb.
In addition is necessary pre-compile the assets before deploy.

Asset Pipeline in production not loading certain assets

When I push my app to heroku certain assets are not loading and I cannot figure out why. One of the things that I noticed is that the stylesheets that are not loading are being loaded with the path:
stylesheets/stylesheet.css
instead of
assets/stylesheet.css
In my css folder I have an .scss file in there, and that seems to be loading just fine but the others are not loading.
This is a rails 4 app.
I've played around with my settings and even installed the following gems:
"rails_12factor"
"rails_serve_static_assets"
"rails_stdout_logging"
My application.rb is as follows:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env)
module myApp
class Application < Rails::Application
end
end`
Production.rb is a bit longer, but for the settings related to assets I have:
config.serve_static_assets = false
config.assets.compile = false
config.assets.digest = true
config.assets.version = 1.0
Does anybody have any suggestions on where to begin in order to diagnose this issue?
Thanks!
Have you tried the steps on the Heroku documentation about precompiling assets? https://devcenter.heroku.com/articles/rails-asset-pipeline

Sass / CSS files getting ignored -- Asset Pipeline issue?

So, of course I figure this out after an hour of messing with my main SASS file and wondering why I couldn't see the changes -- but it turns out that my local host isn't paying any attention to what I'm putting in my SASS files anymore.
This comes just after having switched everything over from SCSS to SASS, and I'm 100% sure the file is just getting ignored. For kicks/proof, I deleted all the styling in my application.erb.sass file (the only one with any styling any more -- I consolidated to get to the bottom of this), then saved, then restarted the server, and it's looking as styled as ever.
This seems to be an asset pipeline issue, and since I don't really know what to do with my config files, I'll paste the relevant-seeming stuff here:
config/environments/development.rb has these lines:
# Do not compress assets
config.assets.compress = false
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
# Expands the lines which load the assets
config.assets.debug = true
config/application.rb
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
# Enable the asset pipeline
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/fonts"
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
Also, I've got "gem 'sass-rails'" in my gem file, not in any group.
Think that's all/most that's relevant. Any idea how to fix this?
run : rake assets:clean && assets:precompile
The problem seems to have been the extension on my css file (which had some embedded ruby):
erb.sass and erb.css.sass didn't work, but css.erb.sass did.
This was inexplicably stopping the file from being read, so that when I ran assets:clean/precompile, there was no css left. Before I ran that, I guess it was running my pre-compiled CSS file from before. Weird.
Apparently, these things must be compiled from the right to the left (as probably everybody but me knew), so CSS should be the type it's compiled into.
Sorry for the confusion.

Resources