[英]Ruby on Rails plugin - Adding validation in class doesn't work properly
[英]Validation doesn't work properly
我有這個模特
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
驗證在控制台中工作正常,但在瀏覽器中卻工作異常。 它不會保存任何沒有名稱的模型,但是它也不會告訴我有關錯誤的信息,表格周圍沒有紅色邊框,沒有任何內容,就像沒有問題一樣進入了列表。 有什么問題和解決方法?
您可能已刪除
<% 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 %>
從形式上偏。 放回去。
所以我通過將此代碼添加到“創建”來解決了這個問題
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.