即使在提交之前,Rails错误消息也始终显示

时间:2013-04-30 02:23:02

标签: ruby-on-rails ruby validation

我的帐户模型:

  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

出于某种原因(仅刚开始发生),表单始终显示验证错误(首先提交或不提交)

为什么会这样?

1 个答案:

答案 0 :(得分:2)

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

@account