2013-07-02 84 views
1

我有这个模型验证不能正常工作

class DishTemplate < ActiveRecord::Base 
    attr_accessible :day_id, :price, :quantity, :restaurant_id, :name, :description, :photo, :photo_cache 
    validates :name, :presence => true 

    mount_uploader :photo, DishPhotoUploader 
end 

和验证在控制台工作正常,但在浏览器,它的工作原理很奇怪。它不会保存任何没有名字的模型,但它也不会告诉我有关错误,没有任何形式的红色边框,没有任何内容,就像没有问题一样进入列表。什么可能是错误的以及如何解决它?

+0

是什么你查看和保存动作看起来像解决这个问题?我猜这是在某个地方的两个地方之一。 –

+0

您根本不会在您的视图中显示您的错误。 @ dishtemplate.errors.full_messages.each做... – kwyoung11

回答

0

,所以我加入这个代码,以“创造”

respond_to do |format| 
    if @dish_template.save 
    format.html { redirect_to admin_restaurant_dish_templates_path(@restaurant), notice: 'template was successfully created.' } 
    format.json { render json: @dish_template, status: :created, location: @dish_template } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @dish_templates.errors, status: :unprocessable_entity } 
    end 
end 
0

你也许去除

<% if @object.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@object.errors.count, "error") %> prohibited this object from being saved:</h2> 

     <ul> 
     <% @object.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

从形式部分。放回去。