# Beginner question about refreshing a page containing an obse

**URL:** <https://www.ruby-forum.com/t/beginner-question-about-refreshing-a-page-containing-an-obse/91532>\
**Category:** Rails\
**Created:** [April 10, 2007, 3:47am UTC](https://www.ruby-forum.com/t/beginner-question-about-refreshing-a-page-containing-an-obse/91532 "2007-04-10T03:47:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bubulle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@bubulle](https://www.ruby-forum.com/u/bubulle)\
**Post date:** [April 10, 2007, 3:47am UTC](https://www.ruby-forum.com/t/beginner-question-about-refreshing-a-page-containing-an-obse/91532/1 "2007-04-10T03:47:19Z")

</div>

Hi all,

I’m still learning the new world of AJAX and RJS and I got hit by a  
problem I could not resolve today.

I have a simple form containing (among other things) a drop\_down list  
(collection\_select with :include\_blank =\> true). This drop\_down list  
is followed by a div that gets populated when the user selects a value  
from the list (I have an observe\_field that triggers a method in my  
controller).

This method has a rjs template file which displays a partial (see code  
below)

When the page is initially displayed, the div is empty. As soon as the  
user makes a selection, some details about the selection show up in  
the view, in the div located under the drop\_down list.

All this works fine unless the user decides to reload the page using  
the browser’s refresh button (or press the F5 key) after having made a  
selection in the list.

In this case, the drop\_down list continues to show the value selected  
before the refresh, however, the div located below the drop\_down list  
is now empty again.

In other words:

- user loads the page for first time
- user makes a selection in drop\_down list =\> info about selection  
shows up in div
- user reloads page =\> selection is still visible, but info about  
selection is not visible anymore

I believe the reason is that the controller’s action that manages the  
page is called again when the user refreshes the page and the model  
associated to the page is reset (i.e. all fields set to nil, since the  
controller does @my\_model = MyModel.new). But the browser did not  
reset the value in the drop\_down list so the list and the div are no  
longer in sync.

Is there a simple way to solve this problem? or am I doing something  
wrong? or am I trying to do something impossible?

Thanks for your help

Rene (aka bubulle)

# code extracts

– my\_view.rhtml

\<%= collection\_select(:my\_model, :stuff\_id , Stuff.find(:all), “id”,  
“name”, :include\_blank =\> true) %\>

\<%= observe\_field ‘my\_model\_stuff\_id’,  
:url =\> {:action =\> ‘show\_stuff\_details’},  
:with =\> “stuff\_id” %\>

– show\_stuff\_details.rjs

page.show ‘my\_info\_div’  
page.replace\_html ‘my\_info\_div’, :partial =\> my\_partial

---

<div class="post-metadata">

**Author:** ![bubulle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@bubulle](https://www.ruby-forum.com/u/bubulle)\
**Post date:** [April 10, 2007, 4:28am UTC](https://www.ruby-forum.com/t/beginner-question-about-refreshing-a-page-containing-an-obse/91532/2 "2007-04-10T04:28:22Z")

</div>

Hi Rene,

bubulle wrote:

> I believe the reason is that the controller’s action that manages the  
> page is called again when the user refreshes the page

Yes.

> Is there a simple way to solve this problem? or am I doing something  
> wrong? or am I trying to do something impossible?

Not impossible at all. You simply need to plan for the refresh. The  
partial is rendered if some value is present in the db, yes? So test  
for  
that in your view.

\<% if some\_stuff\_exists %\>  
\<%= render :partial =\> … %\>  
\<% else %\>  
\<%= %q{

} %\>  
\<%= %q{
} %\>  
\<% end %\>

It’s often cleaner if you just have 2 partials. One with data and the  
other  
empty.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![bubulle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@bubulle](https://www.ruby-forum.com/u/bubulle)\
**Post date:** [April 10, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/beginner-question-about-refreshing-a-page-containing-an-obse/91532/3 "2007-04-10T20:01:50Z")

</div>

Hi Bill,

thanks a lot for your answer. But unfortunately, this does not solve  
my problem because the problem occurs before the user saves anything  
in the database.

Maybe I wasn’t clear enough in my description, so let me try to  
clarify:

I have a view (new.rhtml) corresponding the the new action of my  
controller.  
This view contains a drop\_down list initialized with some values  
(including a blank one) and an empty div under the list.  
The user displays the view, then selects a value. This triggers the  
“observe\_field action” to display some html (for simplicity, let’s say  
it is just text) in the div.  
At this point, the view shows the selected value and the info related  
to that value in the div under the drop down list.  
The user has not pressed the saved button, so there is no data in the  
database.

If the user presses the refresh button, the new action from the  
controller gets called again, but the drop\_down list still shows the  
same value AND the div is empty again.

I believe that the div is empty again because the model behind the  
view is empty (and I do have the same piece of code you suggested) and  
the value in the drop down list is the same as it was before the user  
pressed the refresh button (and I’m not sure why it is so, to be  
honest).

Is there a way to access that value so that I can set the data in the  
div accordingly ?

Any idea why calling the new action resets the div, but not the value  
in the drop down list?

Thanks

Rene
