Rails 3 中的表单验证错误

发布于 2024-10-06 00:12:27 字数 405 浏览 0 评论 0原文

我有一个在 Rails 3 中使用 Formtastic 构建的表单。该表单提交给 ClientsController 的 #create 操作,如果保存成功,控制器将重定向到“谢谢”页面。如果保存不成功,则会重定向到嵌入表单的页面。我希望能够在表单页面上内联显示验证错误,但在重定向回该页面后,错误对象返回为空。

我该怎么做才能在表单上显示验证错误?

这是来自控制器的代码。

if client.save && event.save
  redirect_to "/thank-you"
else
  redirect_to :back
end  

一些其他详细信息:我正在使用 RefineryCMS,因此表单所在的页面没有视图,因此我无法渲染视图。

I have a form I built using Formtastic in Rails 3. The form submits to the #create action of ClientsController, and if the save is successful the controller redirects to a "thank you" page. If the save is not successful, it redirects to the page where the form is embedded. I'd like to be able to show the validation errors inline on the form page, but after the redirect back to that page, the errors object comes back empty.

What can I do so I can display the validation errors on the form?

Here is the code from the controller.

if client.save && event.save
  redirect_to "/thank-you"
else
  redirect_to :back
end  

Some additional details: I am using RefineryCMS so there is no view for the page where the form is, and therefore I can't render the view.

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

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

发布评论

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

评论(2

乖不如嘢 2024-10-13 00:12:27

您需要将错误保存到会话中。


if client.save && event.save
  session[:client_create_errors] = nil
  redirect_to "/thank-you"
else
  session[:client_create_errors] = client.errors
  redirect_to :back
end

现在您可以使用 session[:client_create_errors] 从任何地方访问错误对象

You need to save your errors into session.


if client.save && event.save
  session[:client_create_errors] = nil
  redirect_to "/thank-you"
else
  session[:client_create_errors] = client.errors
  redirect_to :back
end

now you can access errors object from anywere using session[:client_create_errors]

花开柳相依 2024-10-13 00:12:27

为什么不:

render :action => 'name-of-the-view-the-form-is-in'

而不是 redirect_to :back

Why not:

render :action => 'name-of-the-view-the-form-is-in'

instead of the redirect_to :back ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文