/ users处的NoMethodError:nil的未定义方法`email':NilClass

时间:2014-07-09 18:33:34

标签: ruby-on-rails-4 stripe-payments

我知道这可能是Stack上讨论最多的错误,而且我已经过几个小时的其他问题/答案,无法修复它。我确定修复很简单,就在我面前,但我是新的,有点不知所措。我会感激任何指导。

我得到"未定义的方法`电子邮件'为零:NilClass"在为用户的应用创建客户后,在用户的信息经过验证并且信用卡已经处理/批准后。

我使用Stripe,首先在我的StripeWrapper中调用错误(email:options [:user] .email,):

class Customer
 attr_reader :response, :error_message

 def initialize(options={})
   @response = options[:response]
   @error_message = options[:error_message]
 end

 def self.create(options={})
   begin
     response = Stripe::Customer.create(
       card: options[:card],
       email: options[:user].email,
       plan: "base"
     )
     new(response: response)
   rescue Stripe::CardError => e
     new(error_message: e.message)
   end
 end

 def successful?
   response.present?
 end

 def customer_token
   response.id
 end 

然后再次在我的UserSignup对象中(customer = StripeWrapper :: Customer.create):

def initialize(user)
  @user = user
end

def sign_up(stripe_token, invitation_token)
  if @user.valid?
    customer = StripeWrapper::Customer.create(
      :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  

还有我的UsersController (result = UserSignup.new(@user).sign_up(params [:stripeToken],params [:invitation_token]):

  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  

嗯,经过大量的实验,我做到了。 在StripeWrapper中,我改变了

email: options[:user].email

email: options[:stripeEmail]

并将stripeEmail添加为UserSignup def注册中的参数。

我只是在浏览日志时遇到了stripeEmail,以便更好地理解错误。

0 个答案:

没有答案