editing/updating 篇文章中未显示错误 - 按照 rails 指南 v5.1.4
errors not showing upon editing/updating an article - following rails guide v5.1.4
此问题特定于Rails 5.1.4
入门指南中提到的示例教程
根据 rails 入门指南中的 Section 5.11 [Updating Articles],如果文章带有尝试保存空标题或空文本。意思是,应该显示错误消息。
然而,文章被阻止更新(如预期的那样)但错误消息没有显示(如预期的那样)。
是否有人通过 Rails 指南 5.1.4 中解释的入门教程熟悉此问题?
EDIT::: 以下是控制器文件和视图文件。我添加了一些 puts
语句来打印到控制台
articles_controller,rb
文件:
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(valid_article_params)
puts 'article successfully updated...'
redirect_to @article
else
puts 'article could not be updated...'
render 'edit'
end
end
private
def valid_article_params
params.require(:article).permit(:title, :text)
end
edit.html.erb
文件:
<h1>Edit article</h1>
<%= form_with(model: @article) do |form| %>
<% if @article.errors.any? %>
<% puts 'there are some errors here in edit.html.erb' %>
<div id="error_explanation">
<h3>
<%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:
</h3>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% puts 'errors shown from edit.html.erb' %>
<% end %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
问题是您忘记添加 local: true。默认情况下,使用 form_with 时的所有请求都是 ajax。所以你需要把它关掉
form_with(model: @article, local: true)
将 local: true
添加到您的表单中,如下所示:
<%= form_with(model: @article) do |form| %>
收件人:
<%= form_with(model: @article, local: true) do |form| %>
同一个指南中说的原因:
by default form_with submits forms using Ajax thereby skipping full page redirects. To make this guide easier to get into we've disabled that with local: true for now.
我检查了一下,指南有错误。缺少第 5.11 节 local: true
此问题特定于Rails 5.1.4
入门指南中提到的示例教程根据 rails 入门指南中的 Section 5.11 [Updating Articles],如果文章带有尝试保存空标题或空文本。意思是,应该显示错误消息。
然而,文章被阻止更新(如预期的那样)但错误消息没有显示(如预期的那样)。
是否有人通过 Rails 指南 5.1.4 中解释的入门教程熟悉此问题?
EDIT::: 以下是控制器文件和视图文件。我添加了一些 puts
语句来打印到控制台
articles_controller,rb
文件:
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(valid_article_params)
puts 'article successfully updated...'
redirect_to @article
else
puts 'article could not be updated...'
render 'edit'
end
end
private
def valid_article_params
params.require(:article).permit(:title, :text)
end
edit.html.erb
文件:
<h1>Edit article</h1>
<%= form_with(model: @article) do |form| %>
<% if @article.errors.any? %>
<% puts 'there are some errors here in edit.html.erb' %>
<div id="error_explanation">
<h3>
<%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:
</h3>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% puts 'errors shown from edit.html.erb' %>
<% end %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
问题是您忘记添加 local: true。默认情况下,使用 form_with 时的所有请求都是 ajax。所以你需要把它关掉
form_with(model: @article, local: true)
将 local: true
添加到您的表单中,如下所示:
<%= form_with(model: @article) do |form| %>
收件人:
<%= form_with(model: @article, local: true) do |form| %>
同一个指南中说的原因:
by default form_with submits forms using Ajax thereby skipping full page redirects. To make this guide easier to get into we've disabled that with local: true for now.
我检查了一下,指南有错误。缺少第 5.11 节 local: true