简体   繁体   English

在Heroku上载后未初始化的常量错误

[英]Uninitialized constant error after uploading on Heroku

There is the following problem: I'm developing some Rails application on my local machine, and all is good, app works, but after uploading on Heroku there would be the following error (I saw it using 'heroku logs'): 存在以下问题:我正在本地计算机上开发一些Rails应用程序,并且一切正常,应用程序可以运行,但是在Heroku上载后会出现以下错误(我使用“ heroku日志”看到了):

NameError (uninitialized constant Api::V1::ApiV1Controller::UndefinedTokenTypeError)

My code: 我的代码:

  def require_token
    begin
      Some code which generates UndefinedTokenTypeError
    rescue UndefinedTokenTypeError => e
      render json: e.to_json
    end
  end

UndefinedTokenTypeError is in lib/errors.rb file: UndefinedTokenTypeError位于lib / errors.rb文件中:

  class EmptyCookieParamsError < StandardError
    def to_json
      { result_code: 1 }
    end
  end

  class UndefinedTokenTypeError < StandardError
    def to_json
      { result_code: 2 }
    end
  end

I've got the same version for Rails/Ruby on my local machine (2.0). 我在本地计算机(2.0)上安装了相同版本的Rails / Ruby。 How can I fix it? 我该如何解决? Thanks. 谢谢。

Running on Heroku will be using the production environment. 在Heroku上运行将使用生产环境。 Check to see what is different between environments/development.rb and environments/production.rb 检查一下环境/development.rb和环境/production.rb之间的区别

You can try running your app in production mode on your local machine, rails server -e production 您可以尝试在本地计算机( rails server -e production上以生产模式运行应用程序

I am guessing your config.autoload_paths isn't set correctly. 我猜您的config.autoload_paths设置不正确。 Should be in config/application.rb 应该在config / application.rb中

From what I can see, you may be experiencing either a CORS -related issue or you're not authenticating properly 据我所知 ,您可能遇到了与CORS相关的问题,或者您的身份验证不正确


Cross Origin Resource Sharing 跨源资源共享

CORS is a standard HTML protocol , which basically governs which websites can "ping" your site. CORS是标准的HTML协议 ,它基本上控制哪些网站可以“ ping”您的网站。 Facebook & Twitter's third-party widgets only work because they allow any site to send them data Facebook&Twitter的第三方小部件只能工作,因为它们允许任何站点向其发送数据

For Rails to work with CORS, it's recommended to install the Rack-CORS gem . 为了使Rails与CORS一起使用,建议安装Rack-CORS gem This will allow you to put this code in your config/application.rb file: 这将使您可以将此代码放入config/application.rb文件中:

#CORS
config.middleware.use Rack::Cors do
  allow do
     origins '*'
     resource '/data*', :headers => :any, :methods => :post
  end
end

Because you're experiencing these issues on Heroku, it could be the problem you're experiencing. 因为您在Heroku上遇到这些问题,所以可能是您遇到的问题。 Even if it isn't, it's definitely useful to appreciate how CORS works 即使不是,欣赏CORS的工作方式也绝对有用


Authentication 认证方式

Unless your API is public, you'll likely be authenticating the requests 除非您的API是公开的,否则您可能会对请求进行身份验证

The way we do this is with the authenticate_or_request_with_http_token function, which can be seen here: 我们这样做的方法是使用authenticate_or_request_with_http_token函数,可以在这里看到:

  #Check Token
    def restrict_access
        authenticate_or_request_with_http_token do |token, options|
             user = User.exists?(public_key: token)
             @token = token if user
        end
    end

We learnt how to do this with this Railscast , which discusses how to protect an API. 我们学习了如何通过Railscast来做到这一点, 该Railscast讨论了如何保护API。 The reason I asked about your code was because the above works for us on Heroku, and you could gain something from it! 我询问您的代码的原因是因为以上内容对我们在Heroku上有效,您可以从中获得一些好处!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM