ruby-on-rails 尝试在不同的端口加载Rails服务器;但有问题

lpwwtiir  于 5个月前  发布在  Ruby
关注(0)|答案(2)|浏览(63)

我使用的是rails版本5.1.4。我的 Boot .rb文件中有以下代码

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.

require 'rails/commands/server'
module DefaultOptions
  def default_options
    super.merge!(Port: 3020)
  end
end

Rails::Server.send(:prepend, DefaultOptions)

字符串
rails s导致我以下错误
`require ':无法加载此类文件-- rails/commands/server(LoadError)

4urapxun

4urapxun1#

这段代码应该怎么做?

require 'rails/commands/server'
module DefaultOptions
  def default_options
    super.merge!(Port: 3020)
  end
end

Rails::Server.send(:prepend, DefaultOptions)

字符串
实际上virgin config/ Boot .rb 看起来像这样:

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)                                                       

require 'bundler/setup' # Set up gems listed in the Gemfile.


要在不同的端口上运行Rails服务器,您应该通过以下方式运行它:

bundle exec rails server -p 3020

更新时间:

要更改默认端口,可以使用 config/puma.rb。它有相同的行:

port        ENV.fetch('PORT') { 3000 }


换一个吧。

ltskdhd1

ltskdhd12#

如果有人真的遇到了这个问题,并希望在不使用彪马的情况下找到解决方案。
我在railties-5.2.5/lib/rails/commands/server/server_command.rb里面翻了翻,发现了一个可以修改的常量。
这段代码在rails 5.2.8.1中为我工作。

require 'rails/command'
require 'rails/commands/server/server_command'

const = 'DEFAULT_PORT'
Rails::Command::ServerCommand.send(:remove_const, const) if Rails::Command::ServerCommand.const_defined?(const)
Rails::Command::ServerCommand.const_set(const, 3020)

字符串

相关问题