Log
Solving Data Validation Problems in Ruby on Rails
So I’ve jumped on the Ruby on Rails bandwagon and it’s pretty cool for a relative lightweight like myself.
The online documentation and tutorials are great and it’s easy to get something relatively complex up and running in a matter of minutes. I’ve been working on a timesheet application to replace the horrible QuickBooks Timer that I have to run in Virtual PC to record my time at work. Apart from figuring out the old-school QuickBooks .iif data format for export and import, everthing has been going very smoothly and I’ve got a fully functioning system, sans data validation.
So I set out to add my one-line validate statements to the various models. Problem is, they didn’t work for some of the models. If there was invalid data, the program wouldn’t display the error messages and would simply lose the new data.
So here’s the thing: I didn’t use the generate scaffold script for a few of the controllers & views and I didn’t use partial views for the new and edit views. Moreover, I hardcoded the form fields without using the form helpers and I didn’t use error_messages_for. So if the save failed because of invalid data, the view had nowhere to put the error messages nor the updated data.
The solution, of course, is to move the new/edit form into a partial view, _form.rhtml using the form helpers (like the generate scaffold script does), and then use <%= start_form_tag %>, <%= render_partial 'form' %>, and <%= end_form_tag %> to generate the page. That way, the page is fully dynamic, shows the data validation errors and brings your new/modified data with it.
So, other than just not repeating yourself, there’s good reason to use generate scaffold, render_partial and form helpers—the defaults are sensible and you don’t miss them, which saves a lot of headaches.
09/23/05 10:25AM Ruby on Rails
Comments
Hi Andrew,
Unfortunately, I never quite got around to finishing up the system—I got too busy with real work and now use something based on CakePHP to quickly record my time, which I then manually transfer into QuickBooks. I don’t remember exactly what I did in terms of importing the QuickBooks file, but I would think that it would be relatively easy to loop through the lines of the file and compare them to existing records in the database, adding new ones and updating existing ones where necessary.
02/07/07 8:21PM
Add a Comment
Have something to say about what I wrote here? Let’s hear it!
- Your name and email address are required, but your email will not be displayed on the site
- If you provide a URL, a link to your site will appear
- You may use the following HTML:
<strong>bold</strong><em>italic</em><a href="http://url">links</a>
- Double line breaks will be converted to paragraphs.
- As you type, you should get a nice little preview of your comment directly below the text box.
- I reserve the right to edit any comment for any reason (I’ll be reasonable).
Recently Played on iTunes
-
“Heroin”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:26 -
“All Tomorrow's Parties”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:20 -
“Run Run Run”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:16
Andrew Wagner:
I’m currently trying to do essentially the same thing. How did you implement the import of the QuickBooks file?
02/05/07 3:01PM