好的,问题已经改变了。工作实例上的无效创建和更新之间的不同行为是由于在呈现的_form.html.erb中引用了缺少的集合。
@operations = @work.work_ticket.part.operations
我在创建操作中添加了此集合,然后呈现“新”操作。然后,我希望“新”视图在突出显示无效字段的情况下呈现。
但现在出现了新的错误。我得到No route matches {:controller =>“works”}。这似乎很奇怪,因为工作模型的所有路径都有效,包括新操作,除非在失败的保存/验证步骤之后导航到该操作。
以下是可用的路线:
works GET /works(.:format) works#index
POST /works(.:format) works#create
new_work GET /works/new(.:format) works#new
edit_work GET /works/:id/edit(.:format) works#edit
work GET /works/:id(.:format) works#show
PUT /works/:id(.:format) works#update
DELETE /works/:id(.:format) works#destroy
创建动作调用在下面的工作控制器中呈现“new”:
# POST /works
# POST /works.json
def create
@work = Work.new(params[:work])
@operations = @work.work_ticket.part.operations
respond_to do |format|
if @work.save
format.html { redirect_to @work, notice: 'Work was successfully created.' }
format.json { render json: @work, status: :created, location: @work }
else
format.html { render action: "new" }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end
create方法的create操作失败并重新路由到新操作,导致运行时错误。
我不明白为什么更新会正确验证而创建不会。
模型中的验证行如下:
validates :start, :presence => { :message => "must be a valid date/time" }
validates :end, :presence => {:message => "must be a valid date/time"}
validate :start_must_be_before_end_time
def start_must_be_before_end_time
errors.add(:start, "must be before end time") unless self.start < self.end
end
任何指针都会很棒。