简体   繁体   English

ruby on rails错误表单重定向到现有页面并显示错误

[英]ruby on rails error on form redirect to existing page and display errors

So, I am trying to fix up code where we are doing a render in a POST which really screws up the back button horribly for users(and refresh button too!). 所以,我正在尝试修复我们在POST中进行渲染的代码,这实际上为用户搞砸了后退按钮(以及刷新按钮!)。 To get rid of the nasty "Are you sure you want to post again" is typically simply going to the POST->redirect->GET pattern but using some sort of short lived cookie for the errors I need to transfer through(as well as a cookie for what the user typed in as well so he doesn't lose his input). 摆脱讨厌的“你确定要再次发布”通常只是转到POST-> redirect-> GET模式,但使用某种短暂的cookie来处理我需要传输的错误(以及一个用户键入的cookie,所以他不会丢失他的输入)。 In the playframework, this is the flash cookie(which is deleted on the following GET request). 在playframework中,这是flash cookie(在以下GET请求中删除)。

  1. Can I assume in ruby on rails, it is also in flash scope I need? 我可以假设在轨道上的ruby中,它也在我需要的闪存范围内吗?
  2. What is the pattern or example for saving all the errors PER input box such that on the GET, ruby mostly automatically handles the errors for me sticking them in with each field? 什么是保存所有错误PER输入框的模式或示例,以便在GET上,ruby主要自动处理错误,因为我将它们粘贴到每个字段中? - I am using simple_form_for - 我正在使用simple_form_for

I am a complete newb to rails but have tons of experience with much to heavyweight and lightweight http frameworks in java. 我是rails的完全新手,但在java中有很多重量级和轻量级http框架的经验。

A complete example with the GET controller method and POST controller method would be phenomenal if someone knows of an existing one? 如果有人知道现有的一个,那么使用GET控制器方法和POST控制器方法的完整示例将是惊人的吗?

In play, I just call validation.keep() (saves errors) and params.flash() (saves user input) in my post controller method and it automatically maps the errors to my form fields on the GET. 在游戏中,我只是在我的帖子控制器方法中调用validation.keep()(保存错误)和params.flash()(保存用户输入),它会自动将错误映射到GET上的表单字段。 I don't do anything extra. 我不做任何额外的事情。 Basically like this(see the postUser method) https://github.com/deanhiller/timecardz/blob/master/app/controllers/OurPattern.java 基本上这样(参见postUser方法) https://github.com/deanhiller/timecardz/blob/master/app/controllers/OurPattern.java

I am hoping ruby has something that simple too. 我希望ruby有一些简单的东西。

thanks, Dean 谢谢,迪恩

Rails doesn't do this out of the box. Rails并不是开箱即用的。 For what it's worth, it's "standard" to render after a post in Rails. 对于它的价值,在Rails中发布帖子之后呈现它是“标准”。 Code flow is like this: 代码流是这样的:

  1. POST is performed with improper data to the controller 使用不正确的数据向控制器执行POST
  2. Controller pulls up the relevant model instance, changes it to an invalid state, tries save 控制器拉出相关模型实例,将其更改为无效状态,尝试保存
  3. Model object triggers a validation error, save is not persisted 模型对象触发验证错误,保存不会持久化
  4. Error message put in the instances' #errors method 错误消息放在实例的#errors方法中
  5. this same object is passed in to the template to be rendered 这个相同的对象被传递给要渲染的模板
  6. form handler, in this case simple_form_for (a great gem btw) sees #errors is non-empty, and adds css classes and messages however it is configured to do so. 表单处理程序,在这种情况下,simple_form_for(一个伟大的宝石顺便说一句)看到#errors是非空的,并添加了css类和消息,但它配置为这样做。 This should "just work" if nothing funny is going on. 如果没有什么好玩的话,这应该“正常工作”。

So it's actually built in to rails to have the behavior you're trying to work around, since it depends on the model instance being the same throughout render. 所以它实际上内置于rails中以获得您正在尝试解决的行为,因为它取决于整个渲染中的模型实例是相同的。

That said, Rails does have single-use flash parameters, so you could store the posted form data in the flash, and re-update-validate the object on the next request. 也就是说,Rails确实具有一次性闪存参数,因此您可以将发布的表单数据存储在闪存中,并在下一个请求中重新更新验证对象。 Just watch out for code that does any validation-y stuff outside of the main validatiors. 只需注意在主要验证者之外进行任何验证的代码。 ie object.valid? object.valid? and object.save need to always result in the same errors, but there's nothing preventing you from (incorrectly) adding validation errors in a before_save callback, for example. object.save需要总是导致相同的错误,但没有什么能阻止你(错误地)在before_save回调中添加验证错误,例如。

A similar SO post and this PRG github project look very useful to avoid having to go it alone here. 一个类似的SO帖子这个PRG github项目看起来非常有用,以避免在这里单独行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM