Why doesn't “rails s” work from the app directory?

浪子不回头ぞ 提交于 2019-11-28 08:21:35
Joshua Cheek

It seems to think you are not in a rails directory (your output is saying the only valid way to use rails is with rails new).

Depending on your version, Rails identifies this differently. On 3.2, it checks for a file at script/rails. Now that 4.0 has been released, it looks for either script/rails or bin/rails (https://github.com/rails/rails/blob/207fa5c11ddf1cfd696f0eeb07d6466aae9d451e/railties/lib/rails/app_rails_loader.rb#L6)

Presumably you can get around this by creating the file rails in your script directory (if you do not have a script directory, create one in the root of your app):

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rails/commands'

Of course, it's worth wondering why you don't have this file in the first place. Might be worth making sure your rails is the version you want to be using first (rails -v if the version is newer, this post will show you how to create the new app using the older version).

0x4a6f4672

Possible reasons:

  • you are not in a directory that contains a full rails app
  • your bin directory might me empty, try to run rake rails:update:bin (for Rails 4) or rails app:update:bin (Rails 5)

All the above answers didn't help me. What solved my problem for Rails 4 was to run command in the root directory of my application:

rake rails:update:bin

After that running rails s was running as expected.

You likely have not bundled your gems yet:

# from command line
bundle install

If you use rvm or rbenv for instance to keep multiple ruby versions, maybe your default rails version for that specific ruby version is different than the project you are trying to run and therefore it's not being able to detect your application.

To make sure you are using the right rails version you can compare both results. This is what I've got:

$ rails -v
Rails 3.1.0

to

$ bundle exec rails -v
Rails 5.0.0.1

In this case, you can keep the default rails version and then use:

$ bundle exec rails server

Or install the specific rails gem to that very ruby version with:

$ gem install rails -v 5.0.0.1
$ rails -v
Rails 5.0.0.1

And then get it working with the less verbose command:

$ rails s

I hope this becomes helpful to other folks in the same situation!

I had this problem, took me a few minutes to realize I'd forgotten to change active Ruby version with chruby. Different Ruby implied a different Rails version, which looked for the relevant file in another folder.

First check with your location path and then

bundle install

If still does not work, enter

/bin/bash --login
bundle install
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!