找到的解决方案:cPanel 创建的 Ruby On Rails 设置为任何视图返回空白页面

发布于 2024-10-16 15:11:53 字数 2373 浏览 2 评论 0原文

找到解决方案了!

这似乎是 Mongrel + Rails 2.3.8(Rails+cPanel 使用的 Web 服务器)特有的错误。这就是为什么它仅在您从 cPanel 运行应用程序时显示,但在通过脚本/服务器在本地提供应用程序时不显示。

有关更多详细信息,请参阅: https://rails.lighthouseapp.com /projects/8994/tickets/4690-mongrel-doesnt-work-with-rails-238

最终找到的解决方法是将以下文件放入每个新 Rails 2.3.8 应用程序的 /config/initializers 目录中:

https://gist.github.com/471663

添加此初始化程序后,我的 Rails 应用程序将按预期运行。

最初的问题:

我已经阅读了railsforzombies.org 上非常有用的教程,现在准备开始测试RoR 安装。我的网站位于共享主机上,使用 cPanel。我已经使用 cPanel 创建了测试应用程序,并将其设置在 ~/rails_apps/blog 中。我创建了一个重写,将 mydomain.com/testblog/ 重定向到 mydomain.com:12002,这是 cPanel 运行我的 RoR 应用程序的端口。我还通过 cPanel 在开发模式下启动了该应用程序。

如果我访问 mydomain.com/testblog/,我会看到有用的页面,让我知道我的 RoR 应用程序已创建并正在运行。太棒了,对吧?嗯,没那么快。

我按照guides.rubyonrails.org/v2.3.8/getting_started.html上的入门指南进行操作,进入第4步。我已经运行了script/generatecontrollerhomeindex并且在 app/views/home/index.html.erb 编辑结果视图。这只是一个简单的

<h1>Hello rails!</h1>

但是,当我访问 mydomain.com/testblog/home/index (带或不带尾随“/”)时,我的浏览器中出现一个空白页面,什么也没有(查看源代码什么也没有显示)。

为了确保我不会发疯,我在 Rails 应用程序的 /public 目录中放置了一个文本文件,当我访问 mydomain.com/testblog/test.txt 时,它会得到正确的服务。所以我知道 cPanel 的 Apache 重写工作正常。

有什么想法吗?我想我忽略了一些显而易见的事情,但现在我还是一片空白。

作为参考,我在 cPanel 11、Ruby 1.8.7 和 Rails 2.3.8 上运行。我很想运行 Rails 3.0,但共享主机表示目前不行。

编辑添加:

生成上面的家庭控制器和索引操作将以下行添加到我的routes.rb文件中(我已经检查过,它们在那里):

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

所以mydomain.com/testblog/home/index应该映射到app/views /home/index.html.erb。

再次编辑更多调查细节:

我发现了一些可能有助于阐明这一点的内容。如果我在 cPanel 中停止 Rails 应用程序,然后通过 PuTTY 登录并使用 script/server 启动开发服务器,并执行

curl http://0.0.0.0:3000/home/index

开发服务器确实

<h1>Hello Rails!</h1>

app/views/home/index.html 进行响应。 html.erb。所以它在那里工作。当我在 cPanel 中启动 Rails 应用程序(它告诉我它在端口 12002 上运行)时,我从 mydomain.com/testblog/home/index 收到空响应。 我也会收到空响应

curl http://0.0.0.0:12002

如果我通过服务器上的 SSH 运行它, 。所以它看起来像是 cPanel 的一些问题,而不是 Rails 的问题。

Solution found!

This appears to be a bug specific to Mongrel + Rails 2.3.8, the webserver used by Rails+cPanel. This is why it only shows up when you run the app from cPanel, but not when serving it locally via script/server.

For more details, see:
https://rails.lighthouseapp.com/projects/8994/tickets/4690-mongrel-doesnt-work-with-rails-238

The eventual workaround found is to put the following file in the /config/initializers directory of each new Rails 2.3.8 app:

https://gist.github.com/471663

Once I add this initializer, my Rails app functions as expected.

Original question:

I've gone through the very helpful tutorial at railsforzombies.org and am now ready to get a test RoR install going. My site is on a shared host, using cPanel. I've created the test application using cPanel, and it's set up at ~/rails_apps/blog. I created a rewrite that redirects mydomain.com/testblog/ to mydomain.com:12002, which is the port that cPanel has my RoR app running on. I also started the application via cPanel, in development mode.

If I go to mydomain.com/testblog/, I see the helpful page that lets me know that my RoR app has been created and is functioning. Great, right? Well, not so fast.

I'm following along with the Getting Started guide at guides.rubyonrails.org/v2.3.8/getting_started.html, and I get to step 4. I've run script/generate controller home index and edited the resulting view at app/views/home/index.html.erb. It's just a simple

<h1>Hello rails!</h1>

However, when I go to mydomain.com/testblog/home/index (with or without the trailing '/'), I get a blank page in my browser, nothing at all (View Source shows nothing).

To make sure that I'm not going crazy, I put a text file in my rails app's /public directory, and when I go to mydomain.com/testblog/test.txt, it gets served properly. So I know that the Apache rewrite from cPanel is working properly.

Any ideas? I figure I'm overlooking something that's obvious, but I'm drawing a blank for now.

For reference, I'm running on cPanel 11, Ruby 1.8.7 and Rails 2.3.8. I would love to be running Rails 3.0, but the shared host says it's a no-go for now.

edited to add:

Generating the home controller and index action above added the following lines to my routes.rb file (I've checked, they're there):

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

so mydomain.com/testblog/home/index should map to app/views/home/index.html.erb.

Edited again with more investigation details:

I found something that may shed some light on this. If I stop the rails app in cPanel, then log in via PuTTY and start the dev server with script/server and execute

curl http://0.0.0.0:3000/home/index

the dev server does respond with

<h1>Hello Rails!</h1>

from app/views/home/index.html.erb. So it's working there. It's when I start the Rails app in cPanel (which tells me its running on port 12002) that I get an empty response from mydomain.com/testblog/home/index. I also get an empty response from

curl http://0.0.0.0:12002

if I run it via SSH on the server. So it looks like something screwy with cPanel, not with Rails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2024-10-23 15:11:53

您是否确定并删除了应用程序的 app/public 目录中的 index.html 文件?

另外,生成家庭控制器后,您是否设置了routes.rb来识别新的控制器和操作?

Did you make sure and delete your index.html file in the app/public directory of your app?

Also, after generating the home controller, did you setup your routes.rb to recognize the new controller and action?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文