表条目在开发中工作正常,但在生产中没有

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了表条目在开发中工作正常,但在生产中没有相关的知识,希望对你有一定的参考价值。

我有一个产品页面,当您购买产品时,它将重定向到购买页面(收据和下载页面)。

在我的本地计算机上开发一切正常但是当在远程服务器上进行生产购买时,它会向客户收费(使用Stripe),但不会在purchase表中创建条目 - 这不允许客户下载他们的产品。我检查了远程服务器上的psql中的“purchase”表,它没有任何行/条目。

它也应该发送一封电子邮件 - 链接到购买页面,该页面仅适用于开发。

我在本地计算机和远程服务器上使用PostgreSQL。

收费控制器:

class ChargesController < ApplicationController

  def new
    set_meta_tags noindex: true
  end

  def create
    pack = Pack.find(params[:product_id])

    customer = Stripe::Customer.create(
      :email => params[:stripeEmail],
      :source  => params[:stripeToken],
    )

    # Amount in cents
    charge = Stripe::Charge.create(
      :customer    => customer.id,
      :amount      => pack.price_in_cents,
      :description => 'Product Purchase',
      :currency    => 'usd',
    )

    purchase = Purchase.create(
      email: params[:stripeEmail],
      card: params[:stripeToken],
      amount: pack.price_in_cents,
      description: charge.description,
      currency: charge.currency,
      customer_id: customer.id,
      product_id: pack.id,
      uuid: SecureRandom.uuid,
    )

    redirect_to purchase

  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to new_charge_path
  end

end

purchases_controller:

class PurchasesController < ApplicationController

  def show
    @title = 'Purchase Receipt';
    @purchase = Purchase.find_by_uuid(params[:id])
    @pack = Pack.find(@purchase.product_id)
    set_meta_tags noindex: true
  end

end

购买型号:

class Purchase < ApplicationRecord
  attr_accessor :download_token
  after_create :email_purchaser

  def to_param
    uuid
  end

  def email_purchaser
    PurchaseMailer.purchase_receipt(self).deliver
  end

  def Purchase.new_token
    SecureRandom.urlsafe_base64
  end

  def create_download
    self.download_token = Purchase.email.new_token
    update_attribute(:download, Purchase.email(download_token))
    update_attribute(:download_sent_at, Time.zone.now)
  end

end

purchase_mailer:

class PurchaseMailer < ActionMailer::Base
  layout 'purchase_mailer'
  default from: "First Last <myemail@gmail.com>"

  def purchase_receipt purchase
    @purchase = purchase
    mail to: purchase.email, subject: "Thank you for your purchase. Here's your download link, enjoy!"
  end

end

链接到已发送电子邮件中的purchase.show视图:

<%= link_to "DOWNLOAD", purchase_url(@purchase), target: "_blank" %>

以下是服务器日志:

I, [2019-01-11T22:04:21.404919 #17222]  INFO -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af] Started HEAD "/https://mywebsite.com/" for XX.XXX.XXX.XXX at 2019-01-11 22:04:21 +0000
F, [2019-01-11T22:04:21.405538 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af]
F, [2019-01-11T22:04:21.405585 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af] ActionController::RoutingError (No route matches [HEAD] "/https:/mywebsite.com"):
F, [2019-01-11T22:04:21.405611 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af]
F, [2019-01-11T22:04:21.405648 #17222] FATAL -- : [14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/debug_exceptions.rb:63:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:36:in `call_app'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `block in call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `block in tagged'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:26:in `tagged'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/tagged_logging.rb:69:in `tagged'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/request_id.rb:25:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.6/lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/actionpack-5.1.6/lib/action_dispatch/middleware/executor.rb:12:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] vendor/bundle/ruby/2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:522:in `call'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:97:in `process_request'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:157:in `accept_and_process_next_request'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
[14c2eed0-3aae-4ab6-8142-9aa9744819af] /usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:113:in `block in create_thread_and_abort_on_exception'

这是在充电控制器中将!添加到purchase = Purchase.create!(后的服务器日志:

F, [2019-01-15T21:21:12.869293 #24540] FATAL -- : [932479d4-c710-4ac6-9159-8c3fa5299adc] ActiveModel::UnknownAttributeError (unknown attribute 'uuid' for Purchase.):

这是我的routes.rb文件:

  resources :charges
  resources :purchases, only: [:show]

我已经从get 'purchase' => 'purchases#show', as: 'purchase'删除了routes.rb,因为它破坏了我的代码。

它正在使URL执行此操作:https://mywebsite.com/purchase.uuid

而不是这个:https://mywebsite.com/purchase/uuid

这是我的config / environments / production.rb文件:

Rails.application.configure do
config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
  config.assets.js_compressor = :uglifier
  config.assets.compile = false
  config.log_level = :debug
  config.log_tags = [ :request_id ]
  config.action_mailer.perform_caching = false
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  host = 'transverseaudio.com'
  config.action_mailer.default_url_options = { host: host }
  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => 'transverseaudio.com',
    :enable_starttls_auto => true
  }
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new
  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger = ActiveSupport::TaggedLogging.new(logger)
  end
  config.active_record.dump_schema_after_migration = false
end

运行rails routes之后:

            charges GET    /charges(.:format)                                charges#index
                    POST   /charges(.:format)                                charges#create
         new_charge GET    /charges/new(.:format)                            charges#new
        edit_charge GET    /charges/:id/edit(.:format)                       charges#edit
             charge GET    /charges/:id(.:format)                            charges#show
                    PATCH  /charges/:id(.:format)                            charges#update
                    PUT    /charges/:id(.:format)                            charges#update
                    DELETE /charges/:id(.:format)                            charges#destroy
           purchase GET    /purchases/:id(.:format)                          purchases#show
答案

我建议你在交易中包装ChargesController.create。如果Purchase.create中存在某些(例如验证)错误,则最终会在Stripe中的db + real事务中创建一些部分数据。此外,检查purchase是否已实际创建是一个很好的改进:

if purchase
  redirect_to purchase
else 
  render :new, error: purchase.errors
end

然后在生产GUI中,您将清楚地看到问题所在。

此外,在您的日志F, [2019-01-15T21:21:12.869293 #24540] FATAL -- : [932479d4-c710-4ac6-9159-8c3fa5299adc] ActiveModel::UnknownAttributeError (unknown attribute 'uuid' for Purchase.):中,这可能意味着您的迁移未应用于prod,而您只是缺少Purchase中的uuid列。

另一答案

看起来你收到了HEAD请求。你的路线似乎无法处理这个问题。不要担心这是正常的。

这是一次CORS预检检查。在这里阅读更多:https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

Why does a cross-origin HEAD request need a preflight check?

对于rails Try Rack Cors:https://github.com/cyu/rack-cors这可能有所帮助。

基本上,Rack CORS将允许您的应用程序适当地响应预检检查。

以上是关于表条目在开发中工作正常,但在生产中没有的主要内容,如果未能解决你的问题,请参考以下文章

在生产中出现此错误,但在开发中工作正常 - StripeInvalidRequestError: The `line_items` parameter is required in payment m

Firebase pubsub 函数在模拟器中工作,但在生产中不会立即返回

Taiwlind CSS:样式在生产中不加载,在开发中工作。将 Next.js 与 MDX 一起使用

socket.io 连接在生产中失败,在 localhost 中工作

NextJS:生产中未定义的上下文值(在开发中工作正常)

Django中的Send_mail,在shell中工作,在本地工作,不在视图中