# Validation and Re-display with Ajax

**URL:** <https://www.ruby-forum.com/t/validation-and-re-display-with-ajax/123941>\
**Category:** Rails\
**Created:** [December 22, 2007, 10:44am UTC](https://www.ruby-forum.com/t/validation-and-re-display-with-ajax/123941 "2007-12-22T10:44:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajasaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rajasaur](https://www.ruby-forum.com/u/rajasaur)\
**Post date:** [December 22, 2007, 10:44am UTC](https://www.ruby-forum.com/t/validation-and-re-display-with-ajax/123941/1 "2007-12-22T10:44:41Z")

</div>

This might be a simple question but Ive had to work around it in both  
Rails and PHP. Wondering if there is a standard way to do this.

I have a form where you have a Department Dropdown and associated  
Activities for that Department. When you select a department from the  
dropdown, I use ajax to pull the activities for that department and  
display it as another drop down. When I submit this form (that  
contains other text fields) and if validation fails on that, I would  
need to re-display the whole page and also indicate possible errors.  
This is all handled by Rails except for fields that I fetched using  
ajax (activities in the above example). Because the activities is only  
populated when I onchange the department field, the dropdown for  
activities is empty when the page is re-displayed.

Ive worked around this by checking for the department\_id in the  
controller and fetching the activities array during re-display, but as  
you can see, this will get complex when you have a few drop downs  
where each depend on the one above(for e.g. a Country/State/City/Area  
type of dropdown).

Is there a standard pattern for solving this problem?

Thanks  
Raja

---

<div class="post-metadata">

**Author:** ![rajasaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rajasaur](https://www.ruby-forum.com/u/rajasaur)\
**Post date:** [December 22, 2007, 12:07pm UTC](https://www.ruby-forum.com/t/validation-and-re-display-with-ajax/123941/2 "2007-12-22T12:07:05Z")

</div>

afaik thats how you do it … i know your problem and would be  
interested if there are alternatives… but i cannot think of one
