rails active_storage:install fails in app created with --minimal - ruby-on-rails

I made an app with --minimal since standard rails apps are bloated and I would rather add pieces one at a time so I don't end up with unused dependencies, etc.
But when I try to add ActiveStorage, I get the following:
rails aborted!
Don't know how to build task 'active_storage:install' (See the list of available tasks
with `rails --tasks`)
Same result with rails active_storage:install:migrations and app:active_storage:install:migrations.
I have uncommented this line in application.rb:
require "active_storage/engine"
But that didn't solve it. I created another app without the --minimal flag and it works just fine for that one so I am sure I am just missing some piece of the puzzle.
Any help is appreciated!
NOTE: I am using Windows Subsystem for Linux with an Ubuntu distro. Rails 6.1.4

Must have done something wrong because it started working. I think the application.rb file didn't get saved.

Related

Why does carrierwave cause the NameError: uninitialized constant Micropost::PictureUploader error?

In the Hartl Rails tutorial chapter 13.4.1 Basic image upload we add the carrierwave gem for image uploads.
But, I kept getting this error in my tests after generating the uploader and adding the migration to the Microposts model:
NameError: uninitialized constant Micropost::PictureUploader
After googling around I was able to fix this by adding this to my environments.rb:
require 'carrierwave/orm/activerecord'
But I feel like I'm doing something wrong because Hartl does not do this and he assumes at the end of chapter 13.4.1 that your tests should be passing after restarting the server.
Am I doing this wrong?
UPDATE:
After adding that line to my environments.rb file my tests passed. Then once they passed, I was able to remove that line and the tests continued to pass. It's like the constant needed to initialized that one time.
I will say that I forgot to run rails db:migrate before running my tests the first time, but I went on to drop the test database, recreate it, and run the migrations, restart the server and it didn't fix it.
Still confused about what went wrong.
BoraMa and user782220's suggestion of running spring stop then rails test worked for me. No need to add the require line to my environments.rb.
I was also running Hartle's tutorial through JetBrains RubyMine on Windows through WSL. I found out that stopping the application in RubyMine didn't actually stop the server. Every time I stopped and started the server, another Puma instance started to run. I had to restart the entire application for all the latent servers to terminate.
You can check if you have excess servers running by typing ps aux | grep puma into your command line.
Seems to be related to this issue
Any update on this? I ran into the same issue and it was resolved by adding
require 'carrierwave/orm/activerecord'
to my environments.rb.
You can try running the Uploader command again and start your server again. Worked for me.
rails generate uploader Picture

'rails generate model' fails to run

I inherited a Rails application and am having quite a difficult time generating a model. Running 'rails generate model Account' pauses for a moment, but then returns me to the command prompt without the output I would normally expect from a successful operation.
Environment info:
-Rails 4.2.5
-Ruby 2.3.0p, loaded via RVM
I've spent hours trying to debug this myself and have found a few interesting things:
1.) Generating a controller works as expected, only models seem broken
2.) If I create a new Rails project I can generate models as expected
3.) I've verified that spring is not running, using "spring stop"
4.) A similar problem is mentioned in this thread by a user with my same environment. I verified I'm in the proper dir, and also tried running "bundle update" as suggested by a user, but that didn't resolve the problem.
A few other resources allude to this being a problem with RVM, but rolling back to older versions of Ruby haven't made a difference.
Any idea what I'm doing wrong here?
Try the following commands:
bundle config --delete bin
rake rails:update:bin
Was able to reach out to one of the previous developers and learned that I'm dealing with JSONClient models and that's why it isn't working for me. D'oh!

How do I change a Ruby on Rails (4) application name?

Essentially I have a basic app that I would like to use as base for my other projects.
I ran git clone git#site.org:user/app.git newfolder
But when I run my rails app rails s I get the following error:
Migrations are pending; run 'rake db:migrate RAILS_ENV=development' to resolve this issue.
So I run rake db:migrate and start the app again, getting the following error:
I have a sneaking suspicion that it has something to do with the app name as asked in this question but I noticed the solution was provided for Rails 3 and the GitHub project hasn't been updated in two years.
Essentially, I think I have a solution (renaming the app) but I don't know how to do that. However, I may be wrong and I don't know why I am getting this error?
You are getting this error because, one of the css files you are requiring in your application.css is requiring application.css. Go through all the file in your app/assets/stylesheets and make sure that none of the file that is required in application.css is requiring application.css.

How to tell when Rails app is activated by migration?

I'm trying to create a migration for my app, and in this app I'm using a gem that tries to startup a different service upon app startup. Apparently, creating a migration...
rails generate migration AddSomeStuffToTable stuff:string
...activates the app, and this gem which tries to connect to startup the service. It appears that starting up the app via generating a migration makes the service startup unable to connect, so it just keeps sleeping and trying again, never actually running the migration.
In this gem, I've already dealt with this for rake, so this is what I've got so far:
MyService.start unless defined? Rake or defined? IRB
This handles the rake problem (like rake db:migrate, rake db:populate), but how can I handle the creation of migration, which (as far as I know) is not a Rake task?
You could try using environment variables for disabling the service:
MyService.start unless ENV['NO_SERVICE']
And run your command like this:
NO_SERVICE=1 rails generate migration AddSomeStuffToTable stuff:string
However, I doubt this scales well, especially if multiple developers are in the app. A better approach might be to do the reverse of this, to only start the service if a particular env variable is present. However, going this direction, you'd need to make sure your app servers set this variable, for example:
Apache: SetEnv START_SERVICE 1
nginx: env START_SERVICE=1
thin: START_SERVICE=1 thin start

Running Rake Without Shell Access?

For a RoR installation, is there any way to run rake commands without root access?
To put it another way, is there any way to get db:create and db:migrate to be run without root access (perhaps automatically or something)? Or can I run rake commands from a RoR controller?
Take a look at rails-2.X.X/lib/tasks/databases.rake and you can see the code called to create, drop, and migrate your database.
Once a rails environment is initialized, you can use the code inside the rake task file to create, drop, and migrate.
I do not know if you can do this at the controller level before it errors, but you can always try. You could also do it after rails has finished initializing in the environment file.
config/environment.rb
...
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate'))
Well, it is a bit of a chicken-egg problem, you may be able to start your RoR instance without the database created but I doubt it. If your hosting provider is able to host RoR apps, there must be a way for them to run rake for you or to let you run it somehow.
Since it sounds like you are running into troubles with creating the database, is there a way to do it from the hosting control panel? Still, how are you going to migrate your database? Sounds like you might need to look at a new host. I use Slicehost and think they are great :)
Give this code a try:
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
Rake::Task["db:version"].invoke
I just tried it in ./script/console and that worked. It wouldn't work without the require lines.
I use it to call other rake tasks from a rake task (when it's not a pre-req but something that has to happen in the middle).
Note, that won't get you any of the output from the command. If you want that you could just go with good old backticks and run the command like this:
output = `rake db:version`
That'll launch another process, but I don't think there's a problem with that.
Just to be clear, you do not need root access, you need just shell (ssh) access to that machine.
How are you deploying it without access ? If you're using capistrano than you already have shell access and it can run those tasks for you.

Resources