代码之家  ›  专栏  ›  技术社区  ›  barlop Sandeep

如何让Ruby的Regexp在Heroku中工作而不出现内部服务器错误?

  •  0
  • barlop Sandeep  · 技术社区  · 6 年前

    我有这个程序,我已经设法上传到heroku没有错误。但当我在浏览器中打开它时,如果我在其中保留一行使用Regexp的代码,就会得到“内部服务器错误”。如果我把它评论出来就好了。

    我看过关于Heroku给出“内部服务器错误”的类似标题的问题,但这些问题不涉及Regexp,也没有告诉我需要做什么才能使其正常工作。例如 Heroku deployment internal server error 此人使用的是python,他安装的软件包不兼容,这似乎与我的问题无关。这个 Heroku + Django Internal server Error 一个家伙不得不进口一种叫做DJ芹菜的东西。嗯,也许我必须从某个地方进口一些东西,但我不知道是什么,但那不是django,因为我没有使用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,它会显示“内部服务器错误”

    enter image description here

    然而,如果我改变aaa。rb通过注释掉regexp行

    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>
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Tom Lord    6 年前

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

    然而,在这种情况下,我几乎可以肯定的是,问题是您的本地计算机正在使用 ruby版本 >= 2.4.0 (可能 2.5.1 ,哪个是最新的?),而heroku正在运行 ruby版本 2.3.7 从…起 the documentation :

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

    要解决此问题,我建议包括:

    ruby '2.5.1'
    

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

    具体来说,这里的实际问题是 Ruby 2.4.0 added the method Regexp#match? 因此Heroku当前正在抛出一个“未知方法”错误。