为什么“rails s”不能从app目录中运行?

我在我的app文件夹中,但命令rails s无效。 我在Stack Overflow上阅读了不少post,其中大部分内容似乎来自不在其app目录中的用户。

另外,我还构建了一些其他应用程序。 我检查了这些,Rails服务器适用于所有这些应用程序。 这是我唯一无法启动它的地方。

输出which rails

 /Users/jmcrist/.rvm/gems/ruby-2.0.0-p247/bin/rails 

rails s输出rails s

 MacBook-Pro:first_app jmcrist$ rails s Usage: rails new APP_PATH [options] Options: -r, [--ruby=PATH] # Path to the Ruby binary of your choice # Default: /Users/jmcrist/.rvm/rubies/ruby-2.0.0-p247/bin/ruby -b, [--builder=BUILDER] # Path to a application builder (can be a filesystem path or URL) -m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL) [--skip-gemfile] # Don't create a Gemfile [--skip-bundle] # Don't run bundle install -G, [--skip-git] # Skip Git ignores and keeps -O, [--skip-active-record] # Skip Active Record files -S, [--skip-sprockets] # Skip Sprockets files -d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc) # Default: sqlite3 -j, [--javascript=JAVASCRIPT] # Preconfigure for selected JavaScript library # Default: jquery -J, [--skip-javascript] # Skip JavaScript files [--dev] # Setup the application with Gemfile pointing to your Rails checkout [--edge] # Setup the application with Gemfile pointing to Rails repository -T, [--skip-test-unit] # Skip Test::Unit files [--old-style-hash] # Force using old style hash (:foo => 'bar') on Ruby >= 1.9 Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend] # Run but do not make any changes -q, [--quiet] # Suppress status output -s, [--skip] # Skip files that already exist Rails options: -h, [--help] # Show this help message and quit -v, [--version] # Show Rails version number and quit Description: The 'rails new' command creates a new Rails application with a default directory structure and configuration at the path you specify. You can specify extra command-line arguments to be used every time 'rails new' runs in the .railsrc configuration file in your home directory. Note that the arguments specified in the .railsrc file don't affect the defaults values shown above in this help message. 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. 

我正在研究Hartl的Rails教程,他对gemfile做了很多修改。 我想知道这可能是原因吗?

 source 'https://rubygems.org' gem 'rails', '3.2.13' group :development do gem 'sqlite3', '1.3.5' end # Gems used only for assets and not required # in production environments by default. group :assets do gem 'sass-rails', '3.2.5' gem 'coffee-rails', '3.2.2' gem 'uglifier', '1.2.3' end gem 'jquery-rails', '2.0.2' group :production do gem 'pg', '0.12.2' end 

它似乎认为你不在rails目录中(你的输出是说使用rails的唯一有效方法是使用rails new )。

根据您的版本,Rails可以区别对待。 在3.2上,它检查script/rails的文件。 现在4.0已经发布,它会查找script/railsbin/railshttps://github.com/rails/rails/blob/207fa5c11ddf1cfd696f0eeb07d6466aae9d451e/railties/lib/rails/app_rails_loader.rb#L6

大概你可以通过在script目录中创建文件rails来解决这个问题(如果你没有script目录,在应用程序的根目录中创建一个):

 #!/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' 

当然,值得怀疑的是你为什么不首先拥有这个文件。 可能值得确保您的rails是您想要首先使用的版本( rails -v如果版本较新, 这篇文章将向您展示如何使用旧版本创建新应用程序)。

可能的原因:

  • 您不在包含完整rails应用程序的目录中
  • 您的bin目录可能为空,尝试运行rake rails:update:bin (对于Rails 4)或rails app:update:bin (Rails 5)

以上所有答案都没有帮助我。 解决了Rails 4问题的原因是在我的应用程序的根目录中运行命令:

 rake rails:update:bin 

之后运行rails s按预期运行。

你可能还没有捆绑你的gem:

 # from command line bundle install 

我有这个问题,花了几分钟才意识到我忘记用chruby改变活动的Ruby版本。 不同的Ruby暗示了一个不同的Rails版本,它在另一个文件夹中查找相关文件。

首先检查您的位置路径然后

 bundle install 

如果仍然无效,请输入

 /bin/bash --login bundle install 

例如,如果您使用rvmrbenv来保留多个ruby版本,那么该特定ruby版本的默认rails版本可能与您尝试运行的项目不同,因此它无法检测您的应用程序。

要确保使用正确的rails版本,您可以比较两种结果。 这就是我所拥有的:

 $ rails -v Rails 3.1.0 

 $ bundle exec rails -v Rails 5.0.0.1 

在这种情况下,您可以保留默认的rails版本,然后使用:

 $ bundle exec rails server 

或者将特定的rails gem安装到该ruby版本中:

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

然后使用较不详细的命令使其工作:

 $ rails s 

我希望这对同样情况下的其他人有帮助!