创建用户时Heroku路由错误

时间:2014-08-09 18:15:07

标签: ruby-on-rails heroku

我有一个在开发中完美运行的应用程序,一旦部署到Heroku,它的大多数工作正常。邮寄到/users时收到错误,由UsersController#create处理。我似乎无法追查实际问题是什么。

My Heroku Logs:

    2014-08-09T17:44:52.491925+00:00 app[web.1]: Processing by UsersController#new as HTML
    2014-08-09T17:44:52.543342+00:00 app[web.1]: Completed 200 OK in 51ms (Views: 36.4ms |        ActiveRecord: 12.0ms)
    2014-08-09T17:45:27.392552+00:00 heroku[router]: at=info method=POST path="/users"   host=myflix-jirgens.herokuapp.com request_id=c4d84090-0c97-44b5-837a-2986895f4367   fwd="67.189.33.191" dyno=web.1 connect=1ms service=137ms status=500 bytes=1422
    2014-08-09T17:45:27.264626+00:00 app[web.1]: Started POST "/users" for 67.189.33.191    at 2014-08-09 17:45:27 +0000
    2014-08-09T17:45:27.386331+00:00 app[web.1]: Completed 500 Internal Server Error in 119ms
    2014-08-09T17:45:27.276681+00:00 app[web.1]: Processing by UsersController#create as HTML
    2014-08-09T17:45:27.276686+00:00 app[web.1]:   Parameters: {"utf8"=>"✓",   "authenticity_token"=>"JXXaLMfjX7jsKSQ3IBe5lqbtU0TatAN6fdnu8q9shtQ=", "user"=>  {"email"=>"me@example.com", "password"=>"[FILTERED]", "full_name"=>"Me Myself"},    "invitation_token"=>"", "stripeToken"=>"tok_14PduC4NXe5wJuj7ZKn6mImQ",     "stripeTokenType"=>"card", "stripeEmail"=>"me@example.com"}
    2014-08-09T17:45:27.276687+00:00 app[web.1]:   Parameters: {"utf8"=>"✓",     "authenticity_token"=>"JXXaLMfjX7jsKSQ3IBe5lqbtU0TatAN6fdnu8q9shtQ=", "user"=>   {"email"=>"me@example.com", "password"=>"[FILTERED]", "full_name"=>"Me Myself"},    "invitation_token"=>"", "stripeToken"=>"tok_14PduC4NXe5wJuj7ZKn6mImQ",    "stripeTokenType"=>"card", "stripeEmail"=>"me@example.com"}
    2014-08-09T17:45:27.264616+00:00 app[web.1]: Started POST "/users" for 67.189.33.191 at 2014-08-09 17:45:27 +0000
    2014-08-09T17:45:27.276684+00:00 app[web.1]: Processing by UsersController#create as HTML
    2014-08-09T17:45:27.545727+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=myflix-jirgens.herokuapp.com request_id=cc5e62d6-6675-438a-ab57-1b4319ba1351 fwd="67.189.33.191" dyno=web.1 connect=2ms service=8ms status=304 bytes=930
    2014-08-09T17:45:27.386990+00:00 app[web.1]: Completed 500 Internal Server Error in 119ms

我的UsersController:

    class UsersController < InheritedResources::Base
      actions :new, :edit, :update
      before_filter :require_user, only: [:show]

      def create
        @user = User.new(user_params)
        result = UserSignup.new(@user).sign_up(params[:stripeToken], params[:invitation_token])
        if result.successful?
          flash[:success] = "Thank you for registering! Please sign in."
          redirect_to sign_in_path
        else
          flash[:error] = result.error_message
          render :new
        end
      end  
    ...
    end

在Heroku上,我可以填写注册表并提交。页面/users上显示错误“我们很抱歉,但出现了问题”,因此当Heroku在我的本地环境中时,创建功能显然不成功。

请原谅任何新手疏忽。我很感激任何提示。谢谢。

UserSignup.rb:

    class UserSignup

      attr_reader :error_message

      def initialize(user)
        @user = user
      end

      def sign_up(stripe_token, invitation_token)
        if @user.valid?
          customer = StripeWrapper::Customer.create(
            :user => @user,
            :amount => 999, # amount in cents  currency is hardcoded in wrapper
            :card => stripe_token,
            :description => "Subscribtion charge for #{@user.email}"
          )
          if customer.successful?
            @user.customer_token = customer.customer_token
            @user.save
            handle_invitation(invitation_token)
            AppMailer.send_welcome_email(@user).deliver
            @status = :success
            self
          else
            @status = :failed
            @error_message = customer.error_message
            self
          end
        else
          @status = :failed
          @error_message = "Invalid user information. Please check your info."
          self
        end
      end

      def successful?
        @status == :success
      end

      private

      def handle_invitation(invitation_token)
        if invitation_token.present?
          invitation = Invitation.where(token: invitation_token).first
          @user.follow(invitation.inviter)
          invitation.inviter.follow(@user)
          invitation.update_column(:token, nil)
        end
      end
    end

1 个答案:

答案 0 :(得分:0)

评论太长了 -

正如@Mandeep在评论中所述,问题可能UserSignup.new行有关;问题是为什么

我认为有很多方面需要关注:

-

<强>结果

您的行如下:

result = UserSignup.new(@user).sign_up(params[:stripeToken], params[:invitation_token])

这里的问题是您正在尝试使用原始对象构建另一个(看似无关的)对象。这里的核心问题是这条线看起来很简单(抱歉缺乏科学答案)。我将重点关注您的UserUserSignup对象之间的关联,特别是使用after_create回调来填充UserSignup功能:

#app/controllers/users_controller.rb
Class UsersController < InheritedResources::Base
   ...
   def user_params
      params.require(:user).permit(:your, :user, :params).merge(stripe_token: params[:stripeToken], invitation_token: params[:invitation_token])
   end
end

#app/models/user.rb
Class User < ActiveRecord::Base
   attr_accessor :stripe_token, :invitation_token

   before_create :create_user_signup
   after_create  :send_welcome_email

   private

   def create_user_signup
      result = UserSignup.new(self).sign_up(stripe_token, invitation_token)
      if result.successful?
         @user.customer_token = customer.customer_token
         # you might need a "return" here
      else
        errors.add("Stripe Error")
        # likewise, you might need "return false" here
      end
   end

   def send_welcome_email
      AppMailer.send_welcome_email(self).deliver
   end
end

#app/models/user_signup.rb
Class UserSignup
   def sign_up(stripe_token, invitation_token)
      return StripeWrapper::Customer.create(
        :user => @user,
        :amount => 999, # amount in cents  currency is hardcoded in wrapper
        :card => stripe_token,
        :description => "Subscription charge for #{@user.email}"
      )
   end
end

虽然这绝不是经过验证的代码,但我试图为您做的是模块化你的电话。您遇到的一个大问题是您在User

中调用UserSignup模型方法

为了与OOP原则保持一致,您希望确保使用相应对象的相应方法

-

<强>条纹

其次,您要将数据提交到Stripe

这里的问题可能源于几个问题:

  
      
  • 您的Stripe凭据可能无法在Heroku上使用
  •   
  • 您的UserSignup型号可能没有向Stripe发送正确的数据
  •   
  • 您确定Stripe是否在production环境中工作?
  •   

人们对Heroku的核心问题之一就是没有设置environment variables。由于STRIPE需要某些API密钥才能运行,因此您需要确保在后端设置它们

为此,您可以使用Heroku的CLI(Heroku Toolbelt),如下所示:

$ heroku config:set STRIPE_SECRET_KEY=your_secret_key