未显示验证错误 - Rails 4
Validation errors not showing up - Rails 4
出于某种原因,验证错误未显示。
我的表格
<%= form_for [@question.category, @question] do |f| %>
<% if @question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field panel">
<%= f.label :question_type %><br>
<%= f.select :question_type, [ ["single","single"],["multiple","multiple"] ], selected: f.object.question_type %>
</div>
<div class="field panel">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field panel">
<%= f.label :image %><br>
<% if @question.image? %>
<div class="explanation-image text-center">
<%= image_tag @question.image_url(:resized) %>
<p>
<label>
<%= f.check_box :remove_image %>
Remove image
</label>
</p>
</div>
<% end %>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %>
</div>
<div class="field panel">
<%= f.label :explanation %><br>
<%= f.text_area :explanation, size: "30x10" %>
</div>
<div class="field panel">
<%= f.label :link_name %><br>
<%= f.text_field :link_name %>
</div>
<div class="field panel">
<%= f.label :link_url %><br>
<%= f.text_area :link %>
</div>
<div class="field panel">
<%= f.label :video_url %><br>
<%= f.text_area :video_url %>
</div>
<div class="field panel">
<%= f.label :category_id %><br><%= @category.title %>
<%= f.hidden_field :category_id %>
</div>
<div class="actions">
<br>
<%= f.submit 'Submit', class:"button round success" %> <%= link_to 'Back', category_questions_path, class: "button round alert" %>
</div>
<% end %>
这是模型
class Question < ActiveRecord::Base
belongs_to :category
has_many :choices
mount_uploader :image, ImageUploader
validates :description, length: {
minimum: 6
}
validates :link, presence: true
end
和部分控制器
def edit
@category = Category.find(params[:category_id])
@question = @category.questions.find_by(id: params[:id])
end
def update
respond_to do |format|
if @question.update(question_params)
format.html { redirect_to category_question_url(@question.category, @question), notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: @question }
else
format.html {
@category = Category.find(params[:category_id])
@question = @category.questions.find_by(id: params[:id])
render action: :edit
}
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
错误消息代码直接来自脚手架。我没碰过它。如果我尝试编辑问题并保存它,而让我们说 link 字段为空,它将正确地重新加载编辑操作,但不会弹出错误消息。
有什么线索吗?
如果保存失败,请在呈现页面之前重新定义@question 变量。
@question = @category.questions.find_by(id: params[:id])
这将删除保存失败并持有验证错误的对象,并将其替换为从数据库中加载的对象。不要这样做。我想如果你只是删除这一行它可能会工作正常。
出于某种原因,验证错误未显示。
我的表格
<%= form_for [@question.category, @question] do |f| %>
<% if @question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field panel">
<%= f.label :question_type %><br>
<%= f.select :question_type, [ ["single","single"],["multiple","multiple"] ], selected: f.object.question_type %>
</div>
<div class="field panel">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field panel">
<%= f.label :image %><br>
<% if @question.image? %>
<div class="explanation-image text-center">
<%= image_tag @question.image_url(:resized) %>
<p>
<label>
<%= f.check_box :remove_image %>
Remove image
</label>
</p>
</div>
<% end %>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %>
</div>
<div class="field panel">
<%= f.label :explanation %><br>
<%= f.text_area :explanation, size: "30x10" %>
</div>
<div class="field panel">
<%= f.label :link_name %><br>
<%= f.text_field :link_name %>
</div>
<div class="field panel">
<%= f.label :link_url %><br>
<%= f.text_area :link %>
</div>
<div class="field panel">
<%= f.label :video_url %><br>
<%= f.text_area :video_url %>
</div>
<div class="field panel">
<%= f.label :category_id %><br><%= @category.title %>
<%= f.hidden_field :category_id %>
</div>
<div class="actions">
<br>
<%= f.submit 'Submit', class:"button round success" %> <%= link_to 'Back', category_questions_path, class: "button round alert" %>
</div>
<% end %>
这是模型
class Question < ActiveRecord::Base
belongs_to :category
has_many :choices
mount_uploader :image, ImageUploader
validates :description, length: {
minimum: 6
}
validates :link, presence: true
end
和部分控制器
def edit
@category = Category.find(params[:category_id])
@question = @category.questions.find_by(id: params[:id])
end
def update
respond_to do |format|
if @question.update(question_params)
format.html { redirect_to category_question_url(@question.category, @question), notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: @question }
else
format.html {
@category = Category.find(params[:category_id])
@question = @category.questions.find_by(id: params[:id])
render action: :edit
}
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
错误消息代码直接来自脚手架。我没碰过它。如果我尝试编辑问题并保存它,而让我们说 link 字段为空,它将正确地重新加载编辑操作,但不会弹出错误消息。
有什么线索吗?
如果保存失败,请在呈现页面之前重新定义@question 变量。
@question = @category.questions.find_by(id: params[:id])
这将删除保存失败并持有验证错误的对象,并将其替换为从数据库中加载的对象。不要这样做。我想如果你只是删除这一行它可能会工作正常。