如何显示嵌套资源验证的错误消息?
我正在创建一个基本的博客应用程序,当用户尝试提交空白评论时,我遇到了显示错误消息的问题。得到的不是漂亮的错误消息,而是带有正确验证错误的活动记录错误消息。比如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 asActiveRecord::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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有类似的问题。一切似乎都工作正常,但我没有收到任何错误
我找到的解决方案是在article#show而不是视图中构建评论:
并且在您的article#show中不要使用@article.comments.build但使用@comment:
确保您在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:
and in your articles#show don't use @article.comments.build but @comment:
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.
我想你是说你得到了大的白底灰错误页面,对吗?
检查回溯,但我怀疑这是来自控制器中的
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!解决方案是将创建的注释操作引导回正确的控制器/操作并在我的错误消息部分中将目标@comment 定向。
最终视图
评论控制器中的最终创建操作
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
Final create action in comments controller