Rails验证错误显示嵌入式表单的问题

时间:2022-06-11 20:39:59

I have a "projects/show" page for that shows the details of the selected project. Within the show page I have a simple form so that someone can enter updates and comments on a given project. Everything seems to be working but for some reason I cannot get validation working for the form.

我有一个“项目/展示”页面,显示所选项目的详细信息。在展示页面中,我有一个简单的表单,以便有人可以输入给定项目的更新和评论。一切似乎都在起作用,但出于某种原因,我无法为表单进行验证。

I guess I should say that the validation works because the comment is not created, but the error message does not display. Here is gist link to my code:

我想我应该说验证是有效的,因为没有创建注释,但是不显示错误消息。这是我的代码的主要链接:

Gist Link

要点链接

Any ideas on what is wrong?

关于什么是错的任何想法?

1 个解决方案

#1


1  

Problem #1

问题#1

On this line:

在这一行:

<%= form_for([@project, @project.comments.build]) do |f| %>

...you are building a brand new comment every time, regardless of whether this is the initial invocation of the form, or after an invalid submission. This new comment does not have the errors of @comment.

...您每次都在构建一个全新的评论,无论这是表单的初始调用,还是在提交无效之后。这个新评论没有@comment的错误。

Try this instead:

试试这个:

<%= form_for([@project, @comment || @project.comments.build]) do |f| %>

In the new action, @comment will be nil, so a new comment will be built. In the create action, @comment is present already, so it will be used to display the errors and repopulate the form.

在新的行动中,@ comment将为零,因此将建立一个新的评论。在创建操作中,@ comment已经存在,因此它将用于显示错误并重新填充表单。

Alternative (perhaps cleaner) solution:

替代(可能更清洁)解决方案:

# in the controller
def new
  @comment = @project.comments.build
end

# in the template
<%= form_for([@project, @comment]) do |f| %>

Problem #2

问题#2

You always redirect away from the create action, even if there is an error. Instead, you can just re-render the show template again:

即使出现错误,您也始终重定向远离创建操作。相反,您可以再次重新渲染节目模板:

if @comment.save
  redirect_to project_path(@project), notice: 'Comment was successfully created.'
else
  render "projects/show"
end

#1


1  

Problem #1

问题#1

On this line:

在这一行:

<%= form_for([@project, @project.comments.build]) do |f| %>

...you are building a brand new comment every time, regardless of whether this is the initial invocation of the form, or after an invalid submission. This new comment does not have the errors of @comment.

...您每次都在构建一个全新的评论,无论这是表单的初始调用,还是在提交无效之后。这个新评论没有@comment的错误。

Try this instead:

试试这个:

<%= form_for([@project, @comment || @project.comments.build]) do |f| %>

In the new action, @comment will be nil, so a new comment will be built. In the create action, @comment is present already, so it will be used to display the errors and repopulate the form.

在新的行动中,@ comment将为零,因此将建立一个新的评论。在创建操作中,@ comment已经存在,因此它将用于显示错误并重新填充表单。

Alternative (perhaps cleaner) solution:

替代(可能更清洁)解决方案:

# in the controller
def new
  @comment = @project.comments.build
end

# in the template
<%= form_for([@project, @comment]) do |f| %>

Problem #2

问题#2

You always redirect away from the create action, even if there is an error. Instead, you can just re-render the show template again:

即使出现错误,您也始终重定向远离创建操作。相反,您可以再次重新渲染节目模板:

if @comment.save
  redirect_to project_path(@project), notice: 'Comment was successfully created.'
else
  render "projects/show"
end