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

lvmkulzt  于 2022-11-29  发布在  Ruby
关注(0)|答案(8)|浏览(101)

我在我的应用程序文件夹中,但是命令rails s不起作用。我阅读了相当多关于Stack Overflow的帖子,其中大多数似乎是来自不在他们的应用程序目录中的用户。
另外,我还开发了一些其他的应用程序。我检查了这些应用程序,Rails服务器可以运行所有这些应用程序。这是唯一一个我无法启动的应用程序。
which rails的输出:

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

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
62lalag4

62lalag41#

它似乎认为您不在rails目录中(您的输出是说使用rails的唯一有效方式是使用rails new)。
Rails根据你的版本不同,会有不同的识别方式。在3.2版本中,它会检查script/rails的文件。现在4.0版本已经发布,它会查找script/railsbin/rails(https://github.com/rails/rails/blob/207 fa 5c 11 ddf 1cfd 696 f0 eeb 07 d 6466 aae 9d 451 e/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如果版本较新,this post会告诉你如何使用旧版本创建新应用)。

cdmah0mi

cdmah0mi2#

可能的原因:

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

pb3skfrl3#

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

rake rails:update:bin

之后,rails s的运行正常。

inb24sb2

inb24sb24#

例如,如果您使用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

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

anauzrmj

anauzrmj5#

您可能尚未捆绑您的宝石:

# from command line
bundle install
nnsrf1az

nnsrf1az6#

我遇到了这个问题,过了几分钟我才意识到我忘了用chruby来更改活动Ruby版本。不同的Ruby意味着不同的Rails版本,它会在另一个文件夹中查找相关文件。

70gysomp

70gysomp7#

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

bundle install

如果仍然不起作用,请输入

/bin/bash --login
bundle install
qltillow

qltillow8#

这对我很有效。!!!(注意:在Rails应用程序中运行此命令

rake db:migrate RAILS_ENV=development;

运行此命令以生成/bin

rake rails:update:bin

rake app:update:bin

=============================================
然后你会得到这样的屏幕(在那去与Y

bin/rails? (enter "h" for help) [Ynaqdhm]  Y

然后可以使用以下命令启动服务器

rails s

相关问题