'rails generate' commands hang when trying to create a model

Ruby on-RailsRuby on-Rails-4

Ruby on-Rails Problem Overview


I'm new to rails and decided this morning to dump my whole database design/model and start over. And being a noob, I'm sure did it incorrectly.

I removed all the files in db/migrate/ and dropped the tables. And when I tried to generate the first new model class, rails just hung. Off in the weeds for 10 minutes before I hit ^C and tried something else.

This time, I again dropped the tables, moved the whole project to project.bad and ran rails new to start over. Again, after generating the new project with the old name, it hung on the rails generate command (I was using the same project name).

In desperation, I tried creating a new project in the same root, but with another name. Eureka! This worked like a champ, creating controllers and model classes, but I'm completely unable to generate anything using the original project name, in the original project or any newly-created one. What am I missing to get this working again? I don't mind a complete loss at this point, but I'd like to be able to use the original project name again!

Here's what log/development.log looks like:

   (255.5ms)  CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL) ENGINE=InnoDB
   (337.7ms)  CREATE UNIQUE INDEX `unique_schema_migrations`  ON `schema_migrations` (`version`) 
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
   (0.2ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations`

Any idea what's supposed to happen after that last SELECT?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

If your rails generate commands hangs, it is most likely that the generated binstubs of rails are the issue. As you mentioned, you renamed the project.

My educated guess is that some paths in the binstubs were still set to the old project directory but did not exist any longer.

There is a great article on how binstubs work here: https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs

rails 4

To reset the binstubs, just delete your bin/ directory in rails and run:

# generates binstubs for ALL gems in the bundle
bundle install --binstubs

# ...OR, generate binstubs for a SINGLE gem (recommended)
bundle binstubs rake

rails 5/rails 6

To reset the binstubs, just delete your bin/ directory in rails and run:

rake app:update:bin

Why do we need to use the 'rake' command for rails 5 and higher, and not the 'rails' command itself?

Since rails 5 some 'rake' commands are encapsulated within the 'rails' command. However when one deletes 'bin/' directory one is also removeing the 'rails' command itself, thus one needs to go back to 'rake' for the reset since 'rails' is not available any longer but 'rake' still is.

Solution 2 - Ruby on-Rails

Found this over at http://www.dixis.com/?p=754

> For one of my projects I am using rails 4.1 (bleeding edge! yeah :) ) and suddenly noticed, that after opening my laptop in the morning my normal rails commands, like

$> rails c
$> rails g migration Bla name description some_more_fields

> just … were hanging and nothing happened??? Like they were waiting for further input. Upon closer investigation, I assumed that the connection to the spring process was lost/corrupt (I move between networks a lot? maybe that could explain it).

> For those unaware, as I was, spring is a Rails application preloader. It speeds up development by keeping your application running in the background so you don’t need to boot it every time you run a test, rake task or migration. Of course when that connection is lost, or corrupt, it hangs.

> A simple

$> spring stop

> stops the spring server, after which any rails command will restart it automatically. Fixed :)

Solution 3 - Ruby on-Rails

In Rails 5 the binstups are created using the rails command.

I just deleted the bin folder myself and then ran rails app:update:bin which fixed my problems.

> In Rails 5, your app's bin/ directory contains executables that are versioned > like any other source code, rather than stubs that are generated on demand. > > Here's how to upgrade: > bundle config --delete bin # Turn off Bundler's stub generator rails app:update:bin # Use the new Rails 5 executables git add bin # Add bin/ to source control

Solution 4 - Ruby on-Rails

I just had to kill spring.

Before

$ rails generate yaddi-yaddi-yadda
hang...
hang...
hang..
^C

My fix:

$ ps -u {user} | grep spring
123 123456 spring app ...

Find the pid, then kill spring.

$ rails generate yaddi-yaddi-yadda
# success.

Solution 5 - Ruby on-Rails

TL;DR: Restarting computer worked for me.

I had the same problem, and although the chosen answer worked, I wasn't comfortable deleting a bunch of stuff I admittedly don't fully understand. My git status on the bin dir looked like this, after deleting the bin dir and running rails app:update:bin

deleted:    bin/bundle
modified:   bin/rails
modified:   bin/rake
modified:   bin/setup
deleted:    bin/spring
deleted:    bin/webpack
deleted:    bin/webpack-dev-server
deleted:    bin/yarn

I felt like something might come back to bite me later, so after reading the article referenced in the accepted answer (http://www.dixis.com/?p=754) I decided to just restart my computer, as that would fix any networking issues. It worked like a charm.

Solution 6 - Ruby on-Rails

closing re-opening the terminal did the trick for me

Solution 7 - Ruby on-Rails

I had the same problem when trying use rails g controller and it would just hang. I used the same steps @mtrolle suggested:

bundle config --delete bin
rails app:update:bin
git add bin

So when I ran: rails g controller Project index it created the controller, helpers, and index view and GET 'project/index' route as expected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBretView Question on Stackoverflow
Solution 1 - Ruby on-RailsmahatmanichView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDanielView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmtrolleView Answer on Stackoverflow
Solution 4 - Ruby on-RailsTristan HallView Answer on Stackoverflow
Solution 5 - Ruby on-RailsCalebView Answer on Stackoverflow
Solution 6 - Ruby on-RailsAddoView Answer on Stackoverflow
Solution 7 - Ruby on-RailsDavid Patrick DonohueView Answer on Stackoverflow