繁体   English   中英

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

[英]Why doesn't "rails s" work from the app directory?

我在我的应用程序文件夹中,但命令rails s不工作。 我在 Stack Overflow 上阅读了很多帖子,其中大多数似乎来自不在其应用程序目录中的用户。

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

Output which rails

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

Output 的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按预期运行。

你可能还没有捆绑你的宝石:

# from command line
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

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

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

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

bundle install

如果仍然无效,请输入

/bin/bash --login
bundle install

这对我有用。!!!(注意:将此命令运行到 rails 应用程序中

rake db:migrate RAILS_ENV=development;

运行此命令生成/bin

rake rails:update:bin

或者

rake app:update:bin

=============================================

然后你会得到这种屏幕(在那个带有Y的 Go 中)

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

然后你可以使用启动服务器

rails s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM