首页 > 解决方案 > 如何让 Ruby 的 Regexp 在 Heroku 中正常工作而不会出现内部服务器错误?

问题描述

我有这个程序,我已经成功地上传到heroku,没有错误。但是当我在浏览器中打开它时,如果我在那里保留一行使用正则表达式,我会得到“内部服务器错误”。如果我把它注释掉,那很好。

我已经查看了有关 Heroku 给出“内部服务器错误”的类似标题的问题,但它们不涉及 Regexp,并且它们没有给我任何关于我需要做什么才能使其工作的想法。例如Heroku 部署内部服务器错误 ,该人使用 python 并且他安装的包不兼容,这似乎与我的问题无关。这个Heroku + Django 内部服务器错误 一个人必须导入一个叫做 djcelery 的东西。好吧,也许我必须在某个地方导入一些东西,但我不知道是什么,但它不会是 djcelery '因为我没有使用 django。

C:\rubytest\regexinheroku1>dir /b
aaa.rb
config.ru
Gemfile
Gemfile.lock

C:\rubytest\regexinheroku1>

这是aaa.rb

C:\rubytest\regexinheroku1>type aaa.rb
require 'sinatra'

get '/' do
z=Regexp.new('.').match?('qwerty')   
"aaaa"
end

这成功了

C:\rubytest\regexinheroku1>git push heroku master

.... deployed to Heroku

这是问题所在

如果我转到 URL,它会显示“内部服务器错误”

在此处输入图像描述

但是,如果我通过注释掉正则表达式行来更改 aaa.rb

C:\rubytest\regexinheroku1>type aaa.rb
require 'sinatra'

get '/' do
# z=Regexp.new('.').match?('qwerty')   
"aaaa"
end

然后它工作正常,出现一个页面并按预期显示“aaaa”。

如果你想重现这个并且以上还不够,这里是所有步骤

So you see the files

C:\rubytest\regexinheroku2>dir /b
aaa.rb
config.ru
Gemfile

The contents of each file 

C:\rubytest\regexinheroku2>type aaa.rb
require 'sinatra'

get '/' do
z=Regexp.new('.').match?('qwerty')  # if I uncomment this, I get internal server error
"aaaa"
end


C:\rubytest\regexinheroku2>type config.ru
require './aaa'
run Sinatra::Application
C:\rubytest\regexinheroku2>

C:\rubytest\regexinheroku2>type Gemfile
source "http://rubygems.org/"
gem 'sinatra'
gem "haml"

And see the commands I run to get the application successfully deployed

C:\rubytest\regexinheroku2>bundle install
...

C:\rubytest\regexinheroku2>git init
Initialized empty Git repository in C:/rubytest/regexinheroku2/.git/

C:\rubytest\regexinheroku2>git add -A
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.

C:\rubytest\regexinheroku2>git commit -m "sddsfsdfsd"
[master (root-commit) 12cf382] sddsfsdfsd
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.
 4 files changed, 39 insertions(+)
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
 create mode 100644 aaa.rb
 create mode 100644 config.ru

C:\rubytest\regexinheroku2>heroku create
path1 gitmisc done
Creating app... done,   abc
https://abc.herokuapp.com/ | ....

C:\rubytest\regexinheroku2>git push heroku master
....
remote: Verifying deploy... done.
To https://git.heroku.com/abc.git
 * [new branch]      master -> master

C:\rubytest\regexinheroku2>

标签: rubyherokusinatra

解决方案


与所有错误一样,首先要做的是检查日志。日志应该(几乎)总是比面向公众的通用“内部服务器错误”提供更好的错误线索。

但是,在这种情况下,我几乎可以肯定问题是您的本地计算机正在使用ruby​​ 版本>= 2.4.0(可能2.5.1是最新的?),而 heroku 正在运行ruby​​ 版本2.3.7。从文档中

如果您Gemfile不包含 ruby​​ 条目,您将继续MRI 2.3.7Cedar-14堆叠Heroku-16。上Heroku-18你会得到MRI 2.4.4

为了解决这个问题,我建议包括:

ruby '2.5.1'

(或您使用的任何版本)在Gemfile. 这通常是一种很好的做法,因为它可以确保您的本地环境和生产环境是相同的。

具体来说,这里的实际问题是Ruby2.4.0添加了 methodRegexp#match?。所以 Heroku 目前正在抛出“未知方法”错误。


推荐阅读