简体   繁体   中英

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!). 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). In the playframework, this is the flash cookie(which is deleted on the following GET request).

  1. Can I assume in ruby on rails, it is also in flash scope I need?
  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? - I am using 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.

A complete example with the GET controller method and POST controller method would be phenomenal if someone knows of an existing one?

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. I don't do anything extra. Basically like this(see the postUser method) https://github.com/deanhiller/timecardz/blob/master/app/controllers/OurPattern.java

I am hoping ruby has something that simple too.

thanks, Dean

Rails doesn't do this out of the box. For what it's worth, it's "standard" to render after a post in Rails. Code flow is like this:

  1. POST is performed with improper data to the controller
  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
  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. 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.

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. Just watch out for code that does any validation-y stuff outside of the main validatiors. ie 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.

A similar SO post and this PRG github project look very useful to avoid having to go it alone here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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