更改 Rails 3 开发的基本 URL
我知道我将部署到一个环境,其中我的应用程序使用基本 URL 运行,如下所示:
http://someserver/mydepartment/myapp
我的开发环境设置为使用默认的 Rails 配置,如下所示:
http://localhost:3000/myapp
我想对此部署进行建模我的开发环境中的路径。也就是说,我想使用如下所示的基本 URL 进行开发:
http://localhost:3000/mydepartment/myapp
这样,我可以使所有 URL 相对于“/”,并且它们将在两种环境中工作。
我该如何更改它,以便我的应用程序能够在我的开发环境中运行在这条路径上?
我找到的解决方案,但对我不起作用:
- 在
routes.rb
中设置scope
似乎不适用于public 中的静态内容
。 - 使用 Apache 的重写功能。我不想在我的开发盒上安装 Apache。理想情况下,该解决方案可以与 WEbrick 一起使用,尽管我似乎 Mongrel 也能大部分工作(Mongrel 和 Ruby 1.9.2 存在一些问题)。
- 设置
relative_url_root
和类似的建议不适用于 Rails 3。 - 动态生成 CSS/JavaScript 并调整路径以在开发和生产环境之间进行补偿。
I know I'm going to deploy to an environment with my application running with a base URL which looks like this:
http://someserver/mydepartment/myapp
My development environment is set up to use the default Rails configuration, which looks like this:
http://localhost:3000/myapp
I'd like to model this deployment path in my development environment. That is, I'd like to develop with a base URL which looks like this:
http://localhost:3000/mydepartment/myapp
That way, I can make all my URLs relative to "/" and they will work in both environments.
How can I change it so my application will live at this path in my development environment?
Solutions I've found, but don't work for me:
- Setting the
scope
inroutes.rb
doesn't seem to work for the static content inpublic
. - Using Apache's rewriting capabilities. I don't want to install Apache on my development box. Ideally the solution would work with WEbrick, though I seem to have Mongrel mostly working as well (there are some problems with Mongrel and Ruby 1.9.2).
- Setting
relative_url_root
and similar suggestions which don't work with Rails 3. - Dynamically generating CSS/JavaScript and adjusting the paths to compensate between development and production environments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试将 Rails 应用程序机架配置映射到不同的 base_uri。
您需要做的就是将现有的“运行”命令包装在地图块中,
尝试在您的 Rails“config.ru”文件中执行此操作:
现在,当您“rails 服务器”时,应用程序应该位于 <强>本地主机:3000/mydepartment。
不确定这是否会给您带来预期的结果,但值得一试。
You can try mapping your rails app rack config to a different base_uri.
All you need to do is wrap the existing 'run' command in a map block
try doing this in your rails 'config.ru' file:
Now when you 'rails server' the app should be at localhost:3000/mydepartment .
Not sure if this will give you the desired outcome, but worth a try.
以下是如何将 Rails 3.1 应用程序部署到 Apache 中的子目录,替换不再存在的
config.action_controller.relative_url_root
。在
config/routes.rb
中:在 Apache 配置文件中:
它应该可以工作,包括自动将所有资产指向
/my_subdir
。Here’s how you can deploy a Rails 3.1 app to a subdirectory in Apache, replacing
config.action_controller.relative_url_root
which no longer exists.In
config/routes.rb
:In your Apache configuration file:
And it should work, including automatically pointing all your assets to
/my_subdir
.