# Ajax on load?

**URL:** <https://www.ruby-forum.com/t/ajax-on-load/188840>\
**Category:** Rails\
**Created:** [May 19, 2010, 6:34am UTC](https://www.ruby-forum.com/t/ajax-on-load/188840 "2010-05-19T06:34:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 19, 2010, 6:34am UTC](https://www.ruby-forum.com/t/ajax-on-load/188840/1 "2010-05-19T06:34:38Z")

</div>

Hi,

I have a form with a select box and there is a div under the select  
box that gets populated with data collected via ajax, based on the  
value selected in the select box. I use an observe field to accomplish  
this. It all work pretty well except, let’s say, the form gets  
submitted (with a value in the select box) and for some reason the  
validation fails and after the page refresh( the form submission isnt  
ajax), the select box keeps the value that had been selected, however,  
since the div underneath only gets populated and shown on a “change”  
event, is hidden. I would like it to be showing if there is already a  
value in the select box. one thing I can do is simply make an ajax  
call when the page loads and populate/show the div, but it would look  
awkward (the ajax requests show a shadow box type progress bar, this  
is a requirement).

Is there a better way to do this?

Thanks

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 19, 2010, 9:55am UTC](https://www.ruby-forum.com/t/ajax-on-load/188840/2 "2010-05-19T09:55:58Z")

</div>

On 19 May 2010, at 06:33, badnaam wrote:

> call when the page loads and populate/show the div, but it would look  
> awkward (the ajax requests show a shadow box type progress bar, this  
> is a requirement).
> 
> Is there a better way to do this?

Since you have the value in your controller and you simply rerender  
the same views, make your views more intelligent and render the  
appropriate data at page render right away.

Bad code, but just so you get the idea

\<% form\_for @record do |f| %\>  
\<% f.select :some\_id %\>  
…  
\<% end %\>

\<% if @record && @record.some\_id %\>  
render view for value with some\_id  
\<% else %\>  
render default view without any value  
\<% end %\>

Best regards

Peter De Berdt
