'rails generate' commands hang when trying to create a model - ruby-on-rails

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?

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.

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 :)

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

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.

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.

closing re-opening the terminal did the trick for me

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.

Related

Bundler is using a binstub that was created for a different gem.

When I run a rails console using
rails console
everything is fine.
When I run a rails console using
bundle exec rails console
I get the following warning
Bundler is using a binstub that was created for a different gem.
This is deprecated, in future versions you may need to `bundle binstub my_gem` to work around a system/bundle conflict.
my_gem happens to be a gem that I've created that is completely unrelated and not used in the current project directory.
I've tried every solution in this question with no luck:
Bundler is using a binstub that was created for a different gem
I would appreciate any guidance on removing this warning or help understanding how binstubs work so that I can figure out what's going on.
Nowadays it's common for projects to have "specialized" versions of tools. E.g. in some projects the "rails" command may be expected to be run using "spring" (to start up faster).
So it's not uncommon to generate files in your project's 'bin' directory, and then use those versions when running commands, so e.g. instead of
bundle exec rails console
or
bundle exec spring rails console
you could simply expect the following to work correctly
bin/rails console
and not care whether the project needs spring or bundler or zeus or whatever.
So if you don't have 'bin/rails' in your project, you should generate one that suits the project, e.g. using
bin/rake rails:update:bin
If you don't already have bin/rake, you might have to use
bundle exec rake rails:update:bin
(so your bin/rake commands will also get a speedup from using spring)
Some people even put ./bin in their paths, so whenever they run rake (or whatever) they are actually running ./bin/rake if it exists.
Troubleshooting
for project specific tasks, use bin/* files, creating them if needed (e.g. using special rake tasks like in Rails or using bundle binstub <gemname>) - usually those have Bundler specific lines that will make Bundler happy.
for non-project gems (like your gem), find out where it is (e.g. which mygem) and check out it's contents - it's probably using e.g. "bundler/setup" which is confusing Bundler (because bundler expects a local Gemfile file). Maybe your gem is using bundler (it shouldn't if it's a "global" kind of tool and not a "project" tool).
Also, if you're using them, check if tools like RVM and .rbenv are correctly adding their stuff to your bin files (they usually need to setup specific paths)
If you still have questions, it's best to post the contents of the bin file causing problems - it's meant to be a plain Ruby file, so if there's something wrong, it's usually because of the file contents (and not anything else).
More info: https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs
It happened in a project of mine. Because I ran bundle install with another ruby version.
Make sure your rvm is the correctly ruby version.

Running bin/rails generate don't work

Im following a rails tutorial, and when im supposed to run the command: 'bin/rails generate model Article'. An error occurs saying that there isnt such a command.
I'm using 'command prompt with ruby on rails' and in the rails project i can find a Bin folder. Am also using windows 7.
Also What is the difference between running only 'rails generate' instead of running 'bin/rails generate'?
Using rails generate is fine if you have no bin stubs (binaries in the root /bin folder of your project). If you do have bin stubs then it's preferred to use them because they may do additional things specific to your project. But even then, it's (probably) fine to just use rails generate still. The other bin stubs may be a little more necessary to use, though (again, if present) because they tend to be shortcuts to e.g. bundle exec rake.
Rails 4.1 ships with bin stubs. That is, when you generate a Rails 4.1 project it generates bin stubs for you. So this is probably why your tutorial mentioned using them -- they're now there by default. But if you're on an older version of Rails that won't help you much.
The big reason Rails 4.1 includes bin stubs is because Rails uses spring by default now. Spring is an application preloader... that makes it so that when you call e.g. bin/rake ... it will load and keep a running rails environment in the background and then, the 2nd time you call bin/rake it will fork from the running environment giving you almost instantaneous response. So this is an example of "additional things specific to your project" that you get from using bin/rake over just rake and bin/rails over just rails.

Fresh Rails 4 app is not recognizing `rails` commands, insists that I use `rails new`

I've successfully made a new application with the rails new command from a clone of the Rails master branch. The app looks fine, and is definitely Rails 4. But when I try to run any other rails command, the prompt gives me:
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
... etc etc
I've tried rails, bundle exec rails, and bin/rails, all give the same prompt. My steps for creating a new Rails 4 app were:
Clone the Rails master branch to my local machine
Create a new rvm gemset, and use it (1.9.3)
Run rails/railties/bin/rails new myapp --edge -T --skip-index-html
cd myapp and run bundle install and bundle update just to be sure
Run any rails command
What am I doing wrong? rvm-prompt suggests I am using the right gemset. One thing I did notice is that even when I remove jbuilder from my Gemfile and run a bundle update, it still appears in my gem list. Is something wrong with my rvm here?
rails-v output is 'Rails 4.0.0.beta'
See my first comment here, it contains a possible fix : https://github.com/rails/rails/issues/9715
This looks like a bug, report back to ruby on rails issues tracking: https://github.com/rails/rails/issues
I had this issue when my 'bin' folder was missing.

Rails 4 doesn't detect application

After updating to Rails 4, I'm trying to start the server, but it won't start. Simply put, the application doesn't seem to get "detected", like there is no application in there.
~ $ bundle exec rails server
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /app/vendor/ruby-2.0.0/bin/ruby
-b, [--builder=BUILDER] # Path to some application builder (...
-m, [--template=TEMPLATE] # Path to some application template (...
[--skip-gemfile] # Don't create a Gemfile
-B, [--skip-bundle] # Don't run bundle install
What is weird is that the (older) system gem will make it start just fine, but not with Bundler.
Why won't Rails start?
I tried navigating the CLI source code, but I can't grip what's going on.
(Using Edge Rails 4.0.0.beta and Ruby 2.0.0.rc)
In case you're wondering, here's the directory listing:
~ $ ls
app config db Gemfile lib mock Rakefile script tmp
bin config.ru features Gemfile.lock log public README.md spec vendor
Rails 4 did change the way it boots up.
Fortunately, you don't have to run rails-new, since there is a handy task for that.
Rails 4 Release Notes:
6.1  Notable changes
Your app's executables now live in the bin/ dir. Run rake rails:update:bin to get bin/bundle, bin/rails, and bin/rake.
Mostly, I've been had by my own laziness. (Even though laziness is the mother of efficiency ;o) ) I've made a point of not using Bundle when running Rails server.
Rails 4.0.0.beta changes the way it boots up. But, since I was using my "old" version of Rails, an older 4.0.0-beta I had lying around in my local gems, which didn't have the change, I didn't notice when the change was made to my bundle.
Now, when came the time to deploy, the obligatory bundle-exec couldn't boot because my code was used to boot the old way.
The solution
Do like the message says to: bundle exec rails new .. And use Bundle this time! At that point, Rails will generate the boilerplate stuff, namely the missing boot files like ./bin/rails.
It will also update some config files, and try to overwrite some stuff. In my case, everything is checked into source control, so there's a bit of diff'ing and choosing whether to overwrite or not and reverting after.
An maybe better idea (I haven't bothered to) would be to branch out in Git, run the "upgrade" rails-new, overwrite everything, commit that, and then three-way merge back with the main branch.
If your bin folder is already in place, you may just need to run bundle install first. This solved the problem for me after cloning a repo that was working perfectly on another machine.

"could not locate gemfile" when doing bundle install on existing rails project

I've been given an existing rails project that I am trying to play around with. however, when I try to run bundle install or rake db:migrate, I run into problems so essentially, i can't really do anything with the code I've been given.
The biggest problem as I see it right now is the fact that it can't locate my gemfile when I bundle install?
How can I find my gemfile.. is there supposed to be one in the root folder of the application?
Is there another step I need to take to initialize an existing project that someone has just copied and pasted to me? Thanks!
Yes, you should have a Gemfile in the root directory of your app.
If you are developing in a Rails 2 app, you might want to check out the Bundler.io page about Rails 2.3:
http://bundler.io/v1.7/rails23.html
If you are using Rails 3+ you can take a learn from Bundler's page on Rails 3 use:
http://bundler.io/v1.7/rails3.html
If you just need to get started with a Gemfile, go to a different directory and generate a dummy app:
$ rails new temporary-app
Copy over the Gemfile to your directory. It will only have the default gems listed, but you may be able to "discover" your needed gems as you go. If you happen to have a Gemfile.lock file then you can see the gems that you need at the top of the dependency tree.

Resources