簡體   English   中英

Heroku生產中的Rails加載路徑

[英]Rails Load Paths in Heroku Production

我正在嘗試使用在Action Cable Rails 5.1.3中創建的服務對象。 它在開發中起作用,但是抱怨Heroku的生產。

ConversationsController

我的應用程序抱怨生產而不是開發。

class ConversationsController < ApplicationController
  def create
    ...
    @conversation = Stage::ConversationService.existing_conversation?
    ...
  end
end

這是服務對象的路徑。

app/lib/stage/conversation_service.rb

這是服務對象:

module Stage
  class ConversationService
    DIRECT_CONVERSATION = 2

    def initialize(conversation)
      @conversation = conversation
    end

    def self.existing_conversation?(current_user, other_user)
      current_user.conversations.each do |conversation|
        next if conversation.user_conversations.count > DIRECT_CONVERSATION

        conversation.user_conversations.each do |user|
          return Conversation.find(user.conversation_id) if user.user_id == other_user.id
        end
      end
      nil
    end

    def self.new_conversation(current_user, other_user)
      conversation = Conversation.create!
      conversation.user_conversations.create!(user_id: current_user.id, conversation_id: conversation.id)
      conversation.user_conversations.create!(user_id: other_user.id, conversation_id: conversation.id)
      conversation
    end
  end
end

Heroku原木

這是Heroku抱怨NameError (uninitialized constant ConversationsController::Stage):

018-02-16T02:18:41.662824+00:00 heroku[router]: at=info method=POST path="/conversations" host=primetimetran.com request_id=1696e60d-8284-40ae-94a0-2491822affb7 fwd="14.226.243.186,162.158.178.181" dyno=web.1 connect=1ms service=10ms status=500 bytes=1827 protocol=http
2018-02-16T02:18:41.655273+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7] Started POST "/conversations" for 162.158.178.181 at 2018-02-16 02:18:41 +0000
2018-02-16T02:18:41.656695+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7] Processing by ConversationsController#create as HTML
2018-02-16T02:18:41.656740+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"SyLFqMPRJQswBgqso+6VsVsiCdWDUuaXeL54d2SCH/tKwKFLAL29JQCgTzxbHbO48CtJLDg9lfUPcdpiu5ac8w==", "conversation"=>{"user_id"=>"1"}}
2018-02-16T02:18:41.661315+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7] Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms)
2018-02-16T02:18:41.661721+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7]
2018-02-16T02:18:41.661786+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7] NameError (uninitialized constant ConversationsController::Stage):
2018-02-16T02:18:41.661806+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7]
2018-02-16T02:18:41.661830+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7] app/controllers/conversations_controller.rb:5:in `create'
2018-02-16T02:18:41.661831+00:00 app[web.1]: [1696e60d-8284-40ae-94a0-2491822affb7] app/middleware/chat_action_cable.rb:10:in `call'

application.rb中

我覺得我想在生產中為自定義服務對象做些什么...? 我不知道,那只是我的猜測。

# frozen_string_literal: true

require_relative 'boot'
require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Humanbook
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    config.generators do |g|
      g.test_framework :rspec, fixture: true, views: false
      g.fixture_replacement :factory_bot, dir: 'spec/factories'
    end

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.autoload_paths << Rails.root.join('lib')

    config.action_mailer.asset_host = 'http://locahost:3000'
    config.action_mailer.delivery_method = :smtp

    ActionMailer::Base.smtp_settings = {
      user_name: ENV['SENDGRID_USERNAME'],
      password: ENV['SENDGRID_PASSWORD'],
      api_key: ENV['SENDGRID_API_KEY'],
      domain: 'http://locahost:3000',
      address: 'smtp.sendgrid.net',
      port: 587,
      authentication: :plain,
      enable_starttls_auto: true
    }
    config.time_zone = 'Asia/Bangkok'
  end
end

嘗試使用::前綴常量名稱,如下所示:

class ConversationsController < ApplicationController
  def create
    ...
    @conversation = ::Stage::ConversationService.existing_conversation?
    ...
  end
end

該錯誤表明在ConversationController名稱空間中ConversationController常量查找。 ::將強制常量查找從全局名稱空間開始。

我將我的服務放在app/services 不知道這是否有所作為。

另外,我通常會這樣做:

class Stage::ConversationService
  ...
end

代替:

module Stage
  class ConversationService
    ...
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM