Main Content:

Posts tagged as 'ajax'

3 Verified Degradable Deletion 2 comments

April 15

I use scaffold for the basis of all my applications at the moment, it provides a sturdy starting point that you can easily build upon, but there are a few issues that need to be solved before getting started, the first of which is deletion.

The problem with the current method of deletion in scaffolding is it requires upon JavaScript, which you shouldn't really rely upon. While in some cases you can say "my users will definitely have JavaScript enabled" I believe it's best practice to assume otherwise anyway.

You could simply provide a form & button in the show view, but that is not always the best place usability-wise, so I tend to make a separate action just for the form and use JavaScript that will fallback (or degrade) to using it.

Download Screencast: Mirror 1 | Mirror 2 | iPhone Size: 32.0mb, Time: 05:10

1 Automated Client-Side Validation 9 comments

March 31

Having to write separate JavaScript to validate user input before submission of forms can be a right pain, not only is it not very DRY, but it wastes time. Using AJAX you can cheat, harnessing the server-side Rails validation you have already written.

To do this, we use the remote_form_for helper, which uses AJAX to load an RJS result (my my, how many acronyms in this post). The RJS result will be either adding/updating the error list, or redirecting to the new post as normal.

Rails also highlights fields which have errors, so to get this functionality, we can use the visual_effect helper, passing each ID of the field from the errors of the object.

I believe this simple technique has been overlooked in the past, so I hope you find it useful.

Please feel free to leave a comment to let me know of any problems and I'd appreciate any feedback you may have.

Download Screencast: Mirror 1 | Mirror 2 | iPhone Size: 47.5mb, Time: 12:24