我有一个产品页面,当你购买产品时,它会重定向到一个购买页面(收据和下载页面)。
在我的本地计算机上进行开发时一切正常,但当在远程服务器上进行生产时,它会向客户收费(使用stripe),但不会在purchase表中创建条目-这不允许客户下载他们的产品。我检查了远程服务器上psql中的“purchases”表,它没有任何行/条目。
它还应该发送一封电子邮件-链接到购买页面,这只在开发中有效。
我正在本地计算机和远程服务器上使用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

采购控制器:
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

购买邮件:
class PurchaseMailer < ActionMailer::Base
  layout 'purchase_mailer'
  default from: "First Last <[email protected]>"

  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'

以下是在Charges控制器中将!添加到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中出现某些(例如验证)错误,则最终会在db+real transaction in stripe中创建一些部分数据。此外,检查purchase是否已实际创建也是一个很好的改进:

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

然后在产品gui中,您将清楚地看到问题所在。
另外,在您的日志中,这可能意味着您的迁移没有在prod中应用,您只是在F, [2019-01-15T21:21:12.869293 #24540] FATAL -- : [932479d4-c710-4ac6-9159-8c3fa5299adc] ActiveModel::UnknownAttributeError (unknown attribute 'uuid' for Purchase.):中缺少uuid列。

关于ruby-on-rails - 表条目在开发中效果很好,但在生产中效果不佳,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54163578/

10-11 02:30