我发布到我的class DragInterface(object):
"""Implements a `drag` method"""
def drag(self):
"""Drag and drop behavior"""
# Your code goes here
class ResizeInterface(object):
"""Implements a `resize` method"""
def resize(self):
"""Drag and drop resize"""
# Code
class EventHandlerInterface(object):
"""Handles events"""
def handle(self, evt):
# Code
class MyNewSurface(BaseSurface, DragInterface, ResizeInterface):
"""Draggable, resizeable surface"""
# Implement here
操作,并且我尝试做两件事:保存OrdersController#create
和Account
。 Order
正在保存,但Order
不是,我不知道为什么不。
Account
class OrdersController < ApplicationController
before_filter :authenticate_user!
include CurrentCart
before_action :set_cart, only: [:new, :create]
before_action :set_order, only: [:show, :edit, :update, :destroy]
# other actions removed
def create
@order = Order.new(order_params)
@order.add_line_items_from_cart(@cart)
@order.email = current_user.email
@order.address = current_user.address
#@order.created at = @line_items.created_at
@account = Account.where(user_id:current_user.id)
@previous_balance = Account.previous_balance_for_user(current_user)
@account = Account.new(
user_id: current_user.id,
email: current_user.email,
debit: @cart.total_price,
acctbal: @previous_balance - @cart.total_price
)
@account.save
respond_to do |format|
if @order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to store_url, notice: 'Thank you for your
order.' }
format.json { render :show, status: :created, location: @order }
else
format.html { render :new }
format.json { render json: @order.errors, status:
:unprocessable_entity }
end
end
end
private
def set_order
@order = Order.find(params[:id])
end
def order_params
params.require(:order).permit(:name, :address, :email, :pay_type,
:datefor)
end
end
class Account < ActiveRecord::Base
belongs_to :users
belongs_to :order
validates :depotype, :credit, presence: true
DEPOSIT_TYPES = [ "Check", "Credit card", "Purchase order" ]
validates :depotype, inclusion: DEPOSIT_TYPES
def self.previous_balance_for_user(user)
where(user_id: user.id).order(:created_at).pluck(:acctbal).last || 0.0
end
end
我在这里做错了什么?
答案 0 :(得分:0)
很明显,由于验证错误,您的@account.save
失败了。您在Account
中的验证是:
validates :depotype, :credit, presence: true
validates :depotype, inclusion: DEPOSIT_TYPES
...但您还没有在控制器代码中提供:depotype
或:credit
。
这里最大的错误是你没有检查控制器代码中@account.save
的返回,如果它返回false(我确定它是),那么你的代码会忽略并且好像什么都没有错。
您应该在.save
模型时检查返回。
另一种选择,特别是当您在代码中提供模型的所有数据时(即,您没有从用户那里获取数据),就是使用.save!
方法(使用!
)如果出现验证错误,将引发异常。然后你可以纠正它。