[ruby-on-rails] Rails 2.3.4 Persisting Model on Validation Failure

I have a standard html form post that is being persisted and validated with a rails model on the server side.

For the sake of discussion, lets call this a "car" model.

When

car.save 

is invoked the validators fire and set a list of fields in error within the model.

How best can I get an object back that has the fields whose validation failed removed, i.e. such that I don't have to delete the fields on the form the user entered correctly?

The validation patterns they use are great but how do I get a hook to when the validation fails such that I can delete the bogus entries from the model?

I could see manually iterating over the errors list and writing a big case statement but that seems like a hack.

The answer is


In your controller, render the new action from your create action if validation fails, with an instance variable, @car populated from the user input (i.e., the params hash). Then, in your view, add a logic check (either an if block around the form or a ternary on the helpers, your choice) that automatically sets the value of the form fields to the params values passed in to @car if car exists. That way, the form will be blank on first visit and in theory only be populated on re-render in the case of error. In any case, they will not be populated unless @car is set.


Examples related to ruby-on-rails

Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Where do I put a single filter that filters methods in two controllers in Rails Empty brackets '[]' appearing when using .where How to integrate Dart into a Rails app Rails 2.3.4 Persisting Model on Validation Failure How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Rails: Can't verify CSRF token authenticity when making a POST request Uncaught ReferenceError: React is not defined

Examples related to validation

Rails 2.3.4 Persisting Model on Validation Failure Input type number "only numeric value" validation How can I manually set an Angular form field as invalid? Laravel Password & Password_Confirmation Validation Reactjs - Form input validation Get all validation errors from Angular 2 FormGroup Min / Max Validator in Angular 2 Final How to validate white spaces/empty spaces? [Angular 2] How to Validate on Max File Size in Laravel? WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to rails-activerecord

Rails 2.3.4 Persisting Model on Validation Failure Add a reference column migration in Rails 4 Rails 4: List of available datatypes ActiveModel::ForbiddenAttributesError when creating new user find vs find_by vs where Combine two ActiveRecord::Relation objects Float vs Decimal in ActiveRecord ActiveRecord find and only return selected columns Rails update_attributes without save? How to change default timezone for Active Record in Rails?

Examples related to ruby-on-rails-2

Rails 2.3.4 Persisting Model on Validation Failure How I can check if an object is null in ruby on rails 2? Rails ActiveRecord date between