如何在开发中更改 Rails 3 服务器的默认端口? [英] How to change Rails 3 server default port in develoment?
问题描述
在我的开发机器上,我使用端口 10524.所以我以这种方式启动我的服务器:
On my development machine, I use port 10524. So I start my server this way :
rails s -p 10524
有没有办法将默认端口更改为 10524,这样我就不必在每次启动服务器时附加端口?
Is there a way to change the default port to 10524 so I wouldn't have to append the port each time I start the server?
推荐答案
首先 - 不要编辑您的 gem 路径中的任何内容!会影响到所有的项目,以后会出很多问题...
First - do not edit anything in your gem path! It will influence all projects, and you will have a lot problems later...
在你的项目中这样编辑script/rails
:
In your project edit script/rails
this way:
#!/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__)
# THIS IS NEW:
require "rails/commands/server"
module Rails
class Server
def default_options
super.merge({
:Port => 10524,
:environment => (ENV['RAILS_ENV'] || "development").dup,
:daemonize => false,
:debugger => false,
:pid => File.expand_path("tmp/pids/server.pid"),
:config => File.expand_path("config.ru")
})
end
end
end
# END OF CHANGE
require 'rails/commands'
原理很简单 - 您正在修补服务器运行器 - 所以它只会影响一个项目.
The principle is simple - you are monkey-patching the server runner - so it will influence just one project.
更新:是的,我知道有一个包含 bash 脚本的更简单的解决方案:
UPDATE: Yes, I know that the there is simpler solution with bash script containing:
#!/bin/bash
rails server -p 10524
但是这个解决方案有一个严重的缺点——它很无聊.
but this solution has a serious drawback - it is boring as hell.
这篇关于如何在开发中更改 Rails 3 服务器的默认端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!