Rails 4 doesn't detect application - ruby-on-rails

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.

Related

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

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.

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.

What's inside my rails ./bin directory?

Ruby on Rails 4 introduced* the ./bin directory. Inside of ./bin are a few executables: bundle, rails, rake, setup, spring.
My questions are:
What do these executables do? Why do they exist?
When are they invoked?
Can I edit them? Why would I edit them?
I've read the source of these files, and I've tried to Google their purpose, but I can't wrap my head around it. I'm looking for an in-depth explanation.
*I believe many of these files existed before Rails 4, just in different location.
Introduced in Rails 4, the ./bin directory contains your app's "binstubs." Binstubs are wrappers around gem executables, like rails or bundle, which ensures a gem executable is run inside the correct environment for your Rails app.
Binstubs can be used in lieu of bundle exec to run a gem's executable inside your app's environment. For example, instead of typing bundle exec rails scaffold products you can type bin/rails scaffold products. Using binstubs is more flexible than bundle exec, because you don't have to cd to the app's root directory and type bundle exec before everything.
By default, bundle, rails, rake, setup, spring binstubs are created automatically for new rails projects. To make a binstub for a gem executable, just type bundle binstubs name_of_gem_executable. You'll find the new binstub in your ./bin directory.
Some suggest putting ./bin in your shell's search $PATH, so that you don't have to type bin/rails and you can just type rails. This is risky because it depends on ./bin coming before the gem executable's path in $PATH; if you happen to forget this ordering and adjust $PATH such that the gem's executable is found before the binstub wrapper, you could easily invoke the gem's executable -- sans the environmental pretext -- without realizing it.
A bin (short for binary) is nothing more than an application. As you have noticed, these files are ruby files, but they do not have the .rb extension and can be run from your shell just as any shell command, without the need to start any ruby interpreter yourself.
So what do theses programs do? I'm pretty sure you know already what rails rake bundle do.
About spring, it's a gem that keeps your app running in the background (hence its need to be run independently from the app). More infos on their github.
I see no reason to edit these files, but that being said, they're ruby files so you can do whatever you want with them. One example of why you may want editing can be found here.
I personally do put some stuffs in the bin folder. Scripts to connect to remote servers, or ruby scripts I need but that I don't want to run as rake tasks since they're more general than my application.

"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.

How to run "rails s"

I have a problem running rails s in Ubuntu. When I type rails s it doesn't start the server, but instead it outputs:
kyala#ubuntu:~/depot$ rails s
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /home/kyala/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db)enter code here
# Default: sqlite3
-b, [--builder=BUILDER] # Path to an application builder (can be a filesystem path or URL)
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL)
[--dev] # Setup the application with Gemfile pointing to your Rails
checkout
[--edge] # Setup the application with Gemfile pointing to Rails
repository
[--skip-gemfile] # Don't create a Gemfile
-O, [--skip-active-record] # Skip Active Record files
-T, [--skip-test-unit] # Skip Test::Unit files
-J, [--skip-prototype] # Skip Prototype files
-G, [--skip-git] # Skip Git ignores and keeps
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Supress status output
-s, [--skip] # Skip files that already exist
Rails options:
-v, [--version] # Show Rails version number and quit
-h, [--help] # Show this help message and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
Try to regenerate binstubs:
rm bin/*
rake rails:update:bin
It should do the trick.
For newer (5.2+) Rails versions use
rake app:update:bin
When the script folder is missing from the Rails application folder it shows the above error.
I just copied it from another app and it worked for me.
Before running the Rails server, you need to first create a Rails application.
For example, to create a new app call "test_app", run the following:
rails new test_app
Once your application is created, you can cd into the directory and start your server:
cd test_app
rails server
OK guyz just for the closure... this problem occurs only when we delete some(mostly script) folders in the rails app... (may be.. accidentally.).. I had this issue but was in a wrong app folder...
My first hunch would be that you are not in the root of your Rails application.
On our deployment servers, I have to type
./script/rails s
when in the root-folder of my Rails-app. I think that is because bin\rails is not known there.
If that would not work, it seems to me that you are not at all inside a Rails root folder, which would also explain why rails s did not work.
A Rails root project will contain at least the following directories: app, lib, config, script ....
I've seen a similar issue with Rails 2.x apps. They fire up fine with thin, unicorn and such, but to get just the webrick server I've had to run bundle exec script/server (or for the less careful script/server seems to work). I don't know the root issue at play here, but this seems to tide me over as I don't maintain any rails 2.x code (simply running ChiliProject 3.x, etc.).
We had the same problem.
Be sure you run the 'rails' command in the script folder and not the binary 'rails' that is different
script/rails s
It´s the same that if you go to your script folder and run the command:
cd script
./rails s
Check whether the 'script' folder exists in your application structure.
I had the same issue. I had forgotten to run bundle after creating an app.
From the root of your project directory run:
bundle install
Run the following command inside your project folder:
rails s
Your project folder contains node_modules, Gemfile.lock, etc...
While searching for an answer myself, I ended up trying a few things that proved useful to getting rails s to work for me. This resulted in 658 files changed, 102204 insertions, and 149 deletions.
Look at the file that you're in by running ls.
Run git status.
Run git add ..
Run git commit -m "Notate whatever changes you are adding to github repository".
I tried to run git push and git push master but neither work, "go figure." My guess is that you can't push changes that belong to a different file or branch.
HERE's THE SECRET... For some strange reason, I was working in a different file so I had to run a git pull YourOtherFile. This is where everything started making sense.
Now, I ran another git status to understand what was going on within this file. There was modified and untracked content.
Next, cd back into the other file.
Run git status to view all of your modified and untracked files.
Run git add . and git commit -m "Notate your changes to this repository".
Watch the magic happen then run a git push.
Run gem update bundler.
Then I ran gem install rails_12factor.
Run another git status to view your modification.
Run git commit -m "Successfully added gem rails_12Factor".
Run git push.
I had issues with bcrypt being locked at 3.1.11 so I ran gem install 'bcrypt'.
Run gem install rails_12factor yet again. I believe that I had the "f" in "factor" capitalized.
Run gem update.
Run gem install pg.
Run git add ..
Run git commit -m "Updated Gemfile".
Run git push.
Run gem install 'pg' yet again.
I was running into all kind of issues but it was because I was trying to upgrade my gemfile to Rails 5.
Run gem install railties.
Run gem install activesupport.
If your Gemfile was already in another version of Rails (gem 'rails', '4.2.6'), make sure that you keep it there as there was not a significant difference in Rails 5.
Just Run 'bundle update' command and start your application
Try "rails server" instead of the short form. Maybe you have it aliased for some reason.

Resources