2013-04-30 60 views
0

我的账户模型:Rails的错误信息一直显示,甚至在提交

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 

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

这是为什么?

+2

你的'save_with_payment'方法有什么作用?它看起来会试图保存'@ account',并且验证失败。你确定这种行为不应该在“创造”行动吗?否则,对于方法和验证来说,很高兴看到你的'Account'模型。 – 2013-04-30 02:34:06

+0

我添加了模型 – Kristian 2013-04-30 05:21:52

回答

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 
+0

哦,有趣。如果我有3个相关模型正在构建,我应该怎么做?仍然都在创建方法? – Kristian 2013-04-30 05:44:13