Rails 3 中的表单验证错误
我有一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将错误保存到会话中。
现在您可以使用
session[:client_create_errors]
从任何地方访问错误对象You need to save your errors into session.
now you can access errors object from anywere using
session[:client_create_errors]
为什么不:
而不是
redirect_to :back
?Why not:
instead of the
redirect_to :back
?