【问题标题】:How to render validation errors on the page that submitted the form?如何在提交表单的页面上呈现验证错误?
【发布时间】:2011-06-29 05:58:42
【问题描述】:

我在文章/显示页面中有一个 cmets 表格。在此页面中,它显示文章并具有 cmets 表单。

当我提交有验证错误的评论时,我需要它返回文章/显示页面,并在那里显示错误。

我应该将render :action => 'new' 更改为其他内容吗?

在 Comment 控制器中,我尝试了:

  def create
    ...

    if @comment.save?
      redirect_to article_path(@comment.article), :notice => "Posted comment!"
    else
      # render :action => 'new'
      render 'articles/show"
    end
  end

但这会报错,因为应用不知道根据 ID 显示哪篇文章。

编辑:我找到了this 解决方案。方法是使用会话来传递错误。这是正确的方法吗?

【问题讨论】:

    标签: ruby-on-rails-3 validation forms


    【解决方案1】:

    试试这个

    def create
        ...
    
        if @comment.save?
          redirect_to article_path(@comment.article), :notice => "Posted comment!"
        else
          # render :action => 'new'
          @article = @comment.article
          render 'articles/show"
        end
      end`
    

    【讨论】:

    【解决方案2】:

    所以修复你的路由,以便应用确实知道根据 ID 显示什么文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2019-05-08
      • 2021-05-28
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多