我无法显示验证错误,即使它正在验证表单。 以我的部分形式
似乎是问题,@ post返回false表示错误,因此不会显示错误。 但@comment返回true表示错误,但我无法用@comment替换@post,因为它会抛出一个未定义的'comment'错误..
我的部分形式在节目内部开始调用:
%html
%head
%title= @post.title
%body
%h2 Create Comments:
= form_for([@post, @post.comments.build]) do |f|
- if @post.errors.any? # <--- Unsure what to do here..
#error_explanation
%h2
= pluralize(@post.errors.count, "error")
prohibited this cart from being saved:
%ul
- @post.errors.full_messages.each do |msg|
%li= msg
= f.label :Name
%br
= f.text_field :name
%br
%br
= f.label :Text
%br
= f.text_area :text
%br
%br
= f.submit
我的评论控制员:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
respond_to do |format|
if @comment.save
format.html { redirect_to @post }
else
format.html { redirect_to @post }
end
end
end
这是展示文件的展示页面:
%html
%head
%title= @post.title
%body
%strong Author name:
= @post.author_name
%br
%br
%strong Title:
= @post.title
%br
%br
%strong Email:
= @post.email
%br
%br
%strong Description:
= @post.description
%h2 Comments for this Post:
= render :partial => @post.comments
= render :partial => "comments/form"
答案 0 :(得分:1)
您可以在帖子模型上使用validates_associated来检查相关模型的验证错误。
答案 1 :(得分:0)
如果@comment.save
失败,您必须呈现新的操作
if @comment.save
format.html { redirect_to @post }
else
render :action => "new"
end