6

I've been away from Ruby on Rails development for some time, going through several system updates. This occurred on Mac OS X Sierra, with its system version of Ruby (2.0.0p648), and Rails 4.0.2.

Creating a new app (rails new appname) works fine when the appname directory doesn't exist---the command creates the app and populates it as it should. However, despite what all the guidebooks and tutorials say, it fails when such a directory exists (even when it's empty). You also can't create an empty directory, cd into it, and run rails new .

The output of the failed command looks like this:

exist
/Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/lib/rails/generators/app_base.rb:97:in create_root': uninitialized constant Rails::Generators::AppBase::FileUtils (NameError) from (eval):1:in create_root' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/command.rb:27:in run' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in invoke_command' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in block in invoke_all' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in each' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in map' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/invocation.rb:133:in invoke_all' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/group.rb:232:in dispatch' from /Library/Ruby/Gems/2.0.0/gems/thor-0.20.0/lib/thor/base.rb:466:in start' from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/lib/rails/commands/application.rb:43:in <top (required)>' from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68:in require' from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68:in require' from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/lib/rails/cli.rb:15:in <top (required)>' from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68:in require' from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:68:in require' from /Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/bin/rails:9:in <top (required)>' from /usr/bin/rails:22:in load' from /usr/bin/rails:22:in `'

How do I get rails to be able to create the app in an existing directory?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
executor21
  • 4,532
  • 6
  • 28
  • 38

2 Answers2

11

For me, I just needed a way of creating a Rails app inside an existing directory.

The directory already had a .git folder inside it, and I also wanted to use the same git repository which the current app was using for the rails app.

Here's how I did it:

All I had to do was to cd into the folder where the app resides:

cd myapp

And then create the rails app inside the folder, skipping git in the process to avoid git conflict and specifying postgresql as my database:

rails new . --database=postgresql --skip-git

Note: The existing project didn't have a .gitignore file, so since my rails new command skipped git, I had to manually create the .gitignore file.

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
  • Hi, I tried this now and got an error saying, "Can't initialize a new Rails application within the directory of another..." I just want to cleanly add postgresql and skip test – SamuelOdan Jun 22 '23 at 14:34
9

After fruitless online searches for the same problem, I went straight to the reported source: Line 97 of /Library/Ruby/Gems/2.0.0/gems/railties-4.0.2/lib/rails/generators/app_base.rb. That line looks like this:

FileUtils.cd(destination_root) unless options[:pretend]

According to the error message, it can't find FileUtils, treating it as an uninitialized constant. This line is also the only place in the app_base.rb file that uses FileUtils.

I went to the top of the file and added require 'fileutils' to its requirements. I saved it (this required the admin password), and rails new . worked perfectly.

While this fixes the immediate problem, I still have questions. Was this always there, and I haven't noticed, or did it appear after an upgrade as I surmised (most likely from Ruby 1.8 or 1.9 to Ruby 2.0). What changed to cause it?

executor21
  • 4,532
  • 6
  • 28
  • 38
  • Thanks, I ran into the exact same issue today, nearly a year later. Did you ever file an issue with the Rails team? I wonder if this has been fixed in Rails 5? – mpelzsherman Aug 06 '18 at 01:57