0

我的帐户模型:

  def save_with_payment
    if valid?
      customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
      self.stripe_customer_token = customer.id
      save!
    end
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

我的帐户控制器:

  # GET /accounts/new
  # GET /accounts/new.json
  def new
    @account = Account.new
    @company = @account.build_company
    @user = @company.users.build

    if @account.save_with_payment
      redirect_to success_path, :notice => "Thank you for subscribing!"
    else
      render :new
    end

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @account }
    end

  end

出于某种原因(刚刚开始发生),表单总是显示验证错误(有或没有先提交)

为什么是这样?

4

1 回答 1

2

@account.save_with_payment在提交之前执行(并且您不将参数传递给此方法)。代码看起来很奇怪,通常有两种方法newcreate在第一种方法中,您只需找到@account并将其传递给表单查看,在第二种方法中您保存@account

def new
  @account = Account.new
end

def create
  @account = Account.new(params[:account])

  if @account.save
    redirect_to @account
  else
    render :action => 'new'
  end
end
于 2013-04-30T02:35:13.887 回答