[英]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请求中删除)。
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:
代码流是这样的:
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.