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

jmc*_*ist 17 ruby ruby-on-rails gemfile

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

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

产量which rails:

/Users/jmcrist/.rvm/gems/ruby-2.0.0-p247/bin/rails
Run Code Online (Sandbox Code Playgroud)

产量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.
Run Code Online (Sandbox Code Playgroud)

我通过哈特尔的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
Run Code Online (Sandbox Code Playgroud)

Jos*_*eek 27

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

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

大概您可以通过railsscript目录中创建文件来解决这个问题(如果您没有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'
Run Code Online (Sandbox Code Playgroud)

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


0x4*_*672 20

可能的原因:

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

  • 实际上,适用于我正在使用的 Rails 5.1.3 应用程序的是“rake app:update:bin”,而不是“rails app:update:bin”。否则,效果很好。谢谢你! (3认同)

Ale*_*eks 8

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

rake rails:update:bin
Run Code Online (Sandbox Code Playgroud)

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