我有以下模型验证
validates_presence_of :enddate
validates_presence_of :startdate
这是发布的内容
Parameters: {"utf8"=>"✓", "authenticity_token"=>"QczhHQoDU7a0/WKnUnLlzQ9Aob/NQOj1naiwfOYIW1c=",
"billsanddeposit"=>{"name"=>"Test Bill Item",
"itemcategory_id"=>"2", "repeatsevery"=>"2", "startdate"=>"01/24/2013", "enddate"=>"01/31/2013",
"deposit"=>"0", "amount"=>"100"}, "commit"=>"Create Billsanddeposit"}
这些是出现的错误
Enddate can't be blank
Startdate can't be blank
这些是数据库中的日期时间字段,当脚手架最初创建表单时,它具有 date_select。我将其更改为带有 jquery datepicker 的 text_field,现在即使我可以看到上面显示的值正在发布,但我得到了上面的错误,表明它们没有被看到。
这是整个模型
class Billsanddeposit < ActiveRecord::Base
attr_accessible :amount, :bankaccount_id, :deposit, :enddate, :itemcategory_id, :name, :repeatsevery, :repeattype_id, :startdate
validates_presence_of :amount
validates_presence_of :bankaccount_id
validates_presence_of :itemcategory_id
validates_presence_of :enddate
validates_presence_of :name
validates_presence_of :repeatsevery
validates_presence_of :repeattype_id
validates_presence_of :startdate
belongs_to :bankaccount
belongs_to :repeattype
belongs_to :itemcategory
has_many :ledgeritems
end
到目前为止,我所做的只是添加关系和验证。
这是控制器,几乎完全默认
def create
@billsanddeposit = Billsanddeposit.new(params[:billsanddeposit])
respond_to do |format|
if @billsanddeposit.save
@billsanddeposits = Billsanddeposit.joins(:bankaccount).where("bankaccounts.user_id = ?", current_user.id)
@billsanddeposit = Billsanddeposit.new
format.html { redirect_to @billsanddeposit, :notice => 'Billsanddeposit was successfully created.' }
format.json { render :json => @billsanddeposit, :status => :created, :location => @billsanddeposit }
else
@billsanddeposits = Billsanddeposit.joins(:bankaccount).where("bankaccounts.user_id = ?", current_user.id)
format.html { render :action => "index" }
format.json { render :json => @billsanddeposit.errors, :status => :unprocessable_entity }
end
end
end