JRuby:Warbler:Tomcat:Rails 帮助程序未获取带有 WAR 扩展后缀的基本 URL
我正在部署一个打包到“abc.war”中的 JRuby/Rails 应用程序,并且它是与打包到同一 Tomcat 实例中的“def.war”中的 Java 应用程序一起部署的。
我的Rails应用程序中有一个名为“博客”的资源,在routes.rb中具有以下条目
resources :blogs
通过routes.rb中的此条目,我有诸如blogs_path()之类的帮助器,因此我可以使用以下语句:
<%= link_to "Blogs", blogs_path %>
在我的应用程序视图文件中,然后点击生成的链接将显示博客列表。
当此应用程序与 Webrick Web 服务器(JRuby 1.6.4,配置有 Ruby 1.9 和 Rails 3.0.10)独立运行时,它工作正常,因为 http://localhost:3000/blogs 当我点击该链接时会得到一个博客列表)。当我将应用程序部署到另一个基本服务器 URL(例如“http://funky-freddy-101.heroku.com”)时,我不必使用“blogs_path”更改代码,因为 Rails 会自动计算输出基本 URL,blogs_path() 代码在构建由“blogs_path”生成的实际 URL 时会考虑到这一点。
将应用程序部署到“abc.war”文件中的 JRuby/Tomcat 时,基本 URL 从 http://localhost:3000”到“http://localhost:8081/abc”。因此在浏览器中输入“http://localhost:8081/abc/blogs”将会给我一个博客列表,并且“http://localhost:8081/blogs”给出了 404 错误,但是问题是,尽管应用程序的基本 URL 包含 WAR 文件名的后缀(即“abc”),Rails 尚未识别。因此,当调用 blogs_path() 时,仍然会生成“http://localhost:8081/blogs”作为 URL。当我单击该链接时,它会显示标准的 Tomcat 404 页面
。 get Rails 获取以引用“abc”为后缀的基本 URL(WAR 文件的名称),并在 JRuby/Tomcat 部署中正确生成 URL?并且因为该应用程序与 def.war 一起运行,所以我无法将其设为根目录应用程序。
I'm deploying a JRuby/Rails app packed up into "abc.war", and it is deployed with a Java app packed up into "def.war" in the same Tomcat instance.
I have a resource in my Rails application called "Blogs", with the following entry in routes.rb
resources :blogs
With this entry in routes.rb, I have helpers such as blogs_path() so I can use statements such as:
<%= link_to "Blogs", blogs_path %>
in my application view file, and hitting the generated link displays a list of blogs.
When this application runs standalone with the Webrick web server (JRuby 1.6.4, configured with Ruby 1.9, and Rails 3.0.10), it works fine, in that http://localhost:3000/blogs gets me a list of blogs when I hit that link). When I deploy the app to another base server URL such as, say, "http://funky-freddy-101.heroku.com", then I don't have to change the code with "blogs_path", because Rails automagically figures out the base URL and blogs_path() code takes that into account when building the actual URL generated by "blogs_path".
When deploying the app to JRuby/Tomcat in the file "abc.war", the base URL changes from http://localhost:3000" to "http://localhost:8081/abc". So typing "http://localhost:8081/abc/blogs" in the browser will give me a list of blogs, and "http://localhost:8081/blogs" gives me a 404 error. However, the problem is that although the base URL for the application includes the suffix of the WAR file name (i.e. "abc") Rails has not picked up on that and therefore still generates "http://localhost:8081/blogs" as the URL when blogs_path() is called. When I click on the link, it gives me the standard Tomcat 404 page.
Is there any simple way to get Rails to get the base URL suffixed with the reference "abc", the name of the WAR file, and generate URLs appropriately in a JRuby/Tomcat deployment? And because this application is running alongside def.war, I cannot make it the root app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 stackoverflow.com 上发现了类似问题。在文件 /config.ru 中,我执行了以下操作:
在部署新 WAR 之前,我还必须确保清除 Tomcat 创建的用于扩展 WAR 文件的文件夹。
然后我的应用程序就在 /abc URL 范围下运行,无论是独立运行还是在 Tomcat 的 WAR 文件中运行都是一样的。
编辑
还有一件事,您还可以将其添加到您的 config/environments/development.rb 和 config/environments/development.rb (如果在生产模式下):
否则当您在您的视图,它们将生成不带“/abc”的链接。
I found a similar question on stackoverflow.com. In the file /config.ru, I did the following:
I also had to make sure that I blew away the folder created by Tomcat to expand my WAR file before I deployed my new WAR.
Then my app just worked under the /abc URL scope, and it worked the same whether standalone or in a WAR file with Tomcat.
EDIT
One more thing you also add this to your config/environments/production.rb and config/environments/development.rb (if on production mode):
otherwise when you call your helpers such as stylesheet_link_tag in your views, they will generate links without the "/abc".
最好的解决方案是在 Tomcat 下将 web 应用程序作为 ROOT 应用程序运行,这样就不必在所有 URL 中包含“/abc”。
The best solution is to run the webapp as the ROOT app under Tomcat, it will get rid of having to include "/abc" in all your urls.