如何显示嵌套资源验证的错误消息?

发布于 2024-09-27 16:58:19 字数 1869 浏览 0 评论 0原文

我正在创建一个基本的博客应用程序,当用户尝试提交空白评论时,我遇到了显示错误消息的问题。得到的不是漂亮的错误消息,而是带有正确验证错误的活动记录错误消息。比如
CommentsController#create 中的 ActiveRecord::RecordInvalid
验证失败:名称不能为空,电子邮件不能为空

在我的文章/显示视图中,我有以下代码:

<%= form_for([@article, @article.comments.build]) do |f| %>
  <%= render "shared/error_messages", :target => @article %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <p><%= f.submit %></p>
<% end %>

我的错误消息部分如下所示:

    <% if target.errors.any? %>
       <div id="error_explanation">
         <h2><%= pluralize(target.errors.count, "error") %> prohibited this record from                 being saved:</h2>

    <ul>
      <% target.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

我知道答案很简单,但我无法弄清楚。

在评论控制器中创建操作:

def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
respond_to do |format|
  if @comment.save
    format.html { redirect_to(@article, :notice => 'Comment was successfully created.') }
    format.xml  { render :xml => @article, :status => :created, :location => @article }
  else
    format.html { render :action => "articles/show" }
    format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
  end
end

I'm creating a basic blog application and I'm running into issues displaying error messages when a user tries to submit a blank comment. Instead of getting a nice looking error message, an active record error message with the correct validation erorrs. Such as
ActiveRecord::RecordInvalid in CommentsController#create
Validation failed: Name can't be blank, Email can't be blank

In my article/show view I have the following code:

<%= form_for([@article, @article.comments.build]) do |f| %>
  <%= render "shared/error_messages", :target => @article %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <p><%= f.submit %></p>
<% end %>

My error messages partial looks like this:

    <% if target.errors.any? %>
       <div id="error_explanation">
         <h2><%= pluralize(target.errors.count, "error") %> prohibited this record from                 being saved:</h2>

    <ul>
      <% target.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

I know the answer is simple but I can't figure it out.

Create action in comments controller:

def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
respond_to do |format|
  if @comment.save
    format.html { redirect_to(@article, :notice => 'Comment was successfully created.') }
    format.xml  { render :xml => @article, :status => :created, :location => @article }
  else
    format.html { render :action => "articles/show" }
    format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城月光淡如水﹏ 2024-10-04 16:58:19

我有类似的问题。一切似乎都工作正常,但我没有收到任何错误
我找到的解决方案是在article#show而不是视图中构建评论:

@article = Article.find(params[:id])
@comment = @article.comments.build(params[:comment])

并且在您的article#show中不要使用@article.comments.build但使用@comment:

<%= form_for([@article, @comment]) do |f| %>
   <%= render 'shared/error_messages', :object => f.object %>
   <p><%= f.submit %></p>
<% end %>

确保您在comment#create中构建评论为好吧(虽然你真的别无选择:P)

而且,我不知道这是否重要(我对 ruby​​ 还很陌生),但我认为你需要传递 f.object 而不是 @comment。

I had a similar problem. everything seemed to work fine, but I was not getting any errors
The solution i found is to build the comment in article#show instead of the view:

@article = Article.find(params[:id])
@comment = @article.comments.build(params[:comment])

and in your articles#show don't use @article.comments.build but @comment:

<%= form_for([@article, @comment]) do |f| %>
   <%= render 'shared/error_messages', :object => f.object %>
   <p><%= f.submit %></p>
<% end %>

make sure you build the comment in your comment#create as well (you really have no choice though :P)

also, I don't know if it matters (i'm pretty new to ruby), but I think you need to pass f.object instead of @comment.

假情假意假温柔 2024-10-04 16:58:19

我想你是说你得到了大的白底灰错误页面,对吗?

检查回溯,但我怀疑这是来自控制器中的 create 操作,而不是视图。

如果您的控制器使用 save! 并在末尾添加 !,则意味着如果记录无效,它将引发错误。另一方面,save 返回 true 或 false,并允许您使用简单的分支逻辑来决定如何反应。

不过,如果我对 save! 的预感不正确,请发布控制器代码,以便我们可以更深入地挖掘:) 谢谢!

I think you're saying that you get the big grey-on-white error page, right?

Check the backtrace, but I suspect that this is coming from the create action in the controller, not the view.

If your controller uses save! with the ! at the end, that means that it will raise an error if the record is invalid. save, on the other hand, returns true or false and lets you use simple branch logic to decide how to react.

If my hunch on save! isn't right, though, please post controller code so we can dig deeper :) Thanks!

夜还是长夜 2024-10-04 16:58:19

解决方案是将创建的注释操作引导回正确的控制器/操作并在我的错误消息部分中将目标@comment 定向。

最终视图

    <%= form_for([@article, @article.comments.build]) do |f| %>
  <%= render "shared/error_messages", :target => @comment %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <p><%= f.submit %></p>
<% end %>

评论控制器中的最终创建操作

def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.build(params[:comment])
    respond_to do |format|
      if @comment.save
        format.html { redirect_to(@article, :notice => 'Comment was successfully created.') }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { render :action => "articles/show" }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
      end
    end

The solution was to direct the comments created action back to the correct controller/action and target @comment in my error message partial.

Final view

    <%= form_for([@article, @article.comments.build]) do |f| %>
  <%= render "shared/error_messages", :target => @comment %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <p><%= f.submit %></p>
<% end %>

Final create action in comments controller

def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.build(params[:comment])
    respond_to do |format|
      if @comment.save
        format.html { redirect_to(@article, :notice => 'Comment was successfully created.') }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { render :action => "articles/show" }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
      end
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文