错误未与 form_with 一起显示 [英] Error not displaying with form_with

查看:61
本文介绍了错误未与 form_with 一起显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好.

我正在学习教程 http://edgeguides.rubyonrails.org/getting_started.html 我发现以下内容:

我有一个名为 ArticlesController 的控制器.create 方法使用if @ article.save"语句来保存@article 对象,如果出现问题,则呈现new".类似地,更新方法使用if @article.update (article_params)"来更新该文章的记录,如果出现问题,则呈现编辑".

在新视图和编辑视图中 <% if @ article.errors.any?%> 用于判断是否有错误并显示相应的消息,但问题是在新视图中工作正常,但在编辑视图中@article.errors.any 视图?即使有错误也返回 false.

谁能告诉我出了什么问题.非常感谢.

然后我放置了 ArticlesController 类、视图 new.html.erb 和 edit.html.erb,以及文章模型.

<小时>

class ArticlesController <应用控制器定义新@article = Article.new结尾定义编辑@article = Article.find(params[:id])结尾定义更新@article = Article.find(params[:id])如果@article.update(article_params)重定向到@article别的渲染编辑"结尾结尾定义索引@articles = Article.all结尾高清秀@article = Article.find(params[:id])结尾定义创建@article = Article.new(article_params)如果@article.save重定向到@article别的呈现新"结尾结尾私人的def article_paramsparams.require(:article).permit(:title, :text)结尾结尾------------------------------------------------------------------<h1>新文章</h1><%= form_with scope: :article, url:articles_path, local: true do |form|%><% if @article.errors.any?%><div id="error_explanation"><h2><%=pluralize(@article.errors.count, "error") %>禁止这篇文章被保存:<ul><% @article.errors.full_messages.each 做 |msg|%><li><%=msg%></li><%结束%>

<%结束%><p><%= form.label :title %><br><%= form.text_field :title %></p><p><%= form.label :text %><br><%= form.text_area :text %></p><p><%=form.submit %></p><%结束%><%= link_to '返回',articles_path %>----------------------------------------------------------------------<h1>编辑文章</h1><%= form_with(model: @article) do |form|%><% if @article.errors.any?%><div id="error_explanation"><h2><%=pluralize(@article.errors.count, "error") %>禁止这篇文章被保存:<ul><% @article.errors.full_messages.each 做 |msg|%><li><%=msg%></li><%结束%>

<%结束%><p><%= form.label :title %><br><%= form.text_field :title %></p><p><%= form.label :text %><br><%= form.text_area :text %></p><p><%=form.submit %></p><%结束%><%= link_to '返回',articles_path %>---------------------------------------------------------------------类文章<申请记录验证:标题,存在:真实,长度:{ 最小值:5 }验证:文本,存在:真结尾

解决方案

默认情况下,form_with 生成的所有表单都将通过 XHR (Ajax) 请求提交.如果你想禁用远程表单,那么你可以像在 new.html.erb 中的表单一样使用本地来完成.在您的 edit.html.erb 中更改:

<%= form_with 模型:@article,local:true do |form|%>

Good Morning.

I'm following the tutorial http://edgeguides.rubyonrails.org/getting_started.html and I find the following:

I have a controller called ArticlesController. The create method uses the "if @ article.save" statement to save the @article object, and if something goes wrong then render 'new'. Similarly the update method uses "if @article.update (article_params)" to update the record for that article, and if something goes wrong render 'edit'.

In new and edit views <% if @ article.errors.any? %> is used to determine if there was any error and display the corresponding message, but the problem is that in the new view works fine, but in the edit view @article.errors.any view? returns false even if there is an error.

Can someone tell me what's wrong. Thank you very much.

Then I put the ArticlesController class, the views new.html.erb and edit.html.erb, and the article model.


class ArticlesController < ApplicationController
  def new
    @article = Article.new
  end

  def edit
    @article = Article.find(params[:id])
  end

  def update
    @article = Article.find(params[:id])

    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
  end

  def index
    @articles = Article.all
  end

  def show
    @article = Article.find(params[:id])
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end

  private

  def article_params
    params.require(:article).permit(:title, :text)
  end
end

    ------------------------------------------------------------------

    <h1>New Article</h1>

    <%= form_with scope: :article, url: articles_path, local: true do |form| %>

      <% if @article.errors.any? %>
        <div id="error_explanation">
          <h2>
            <%= pluralize(@article.errors.count, "error") %> prohibited
            this article from being saved:
          </h2>
          <ul>
            <% @article.errors.full_messages.each do |msg| %>
              <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
      <% 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 %>

    ----------------------------------------------------------------------

    <h1>Edit article</h1>

    <%= form_with(model: @article) do |form| %>

      <% if @article.errors.any? %>
        <div id="error_explanation">
          <h2>
            <%= pluralize(@article.errors.count, "error") %> prohibited
            this article from being saved:
          </h2>
          <ul>
            <% @article.errors.full_messages.each do |msg| %>
              <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
      <% 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 %>

    ---------------------------------------------------------------------

    class Article < ApplicationRecord
        validates :title, presence: true,
                        length: { minimum: 5 }
        validates :text, presence: true
    end

解决方案

All forms generated by form_with will be submitted by an XHR (Ajax) request by default. if you want to disable remote forms then you can do it with local like you did in the form in new.html.erb. change this in your edit.html.erb :

<%= form_with model: @article, local: true do |form| %>

这篇关于错误未与 form_with 一起显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆