# Collection\_select reseting selection whenever page refreshes

**URL:** <https://www.ruby-forum.com/t/collection-select-reseting-selection-whenever-page-refreshes/148071>\
**Category:** Rails\
**Created:** [September 8, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/collection-select-reseting-selection-whenever-page-refreshes/148071 "2008-09-08T23:17:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [September 8, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/collection-select-reseting-selection-whenever-page-refreshes/148071/1 "2008-09-08T23:17:18Z")

</div>

I have a collection\_select dialog that I have for selecting Users on a  
page. I am wanting the selection to remember and display the last  
selected user from the database. Every time I refresh the page it is  
going back to its default value even though the data has changed. Is  
there a way to tell it to take the data that appears in the database  
and not reset the selection tag like it a new value.

Here is my code that I’m using.  
\<% @users = User.list\_all\_testers %\>  
\<%= collection\_select(:machine, :user\_id, @users, :id, :fullname,  
options = {:include\_blank=\>true}, html\_options = {:onChange =\>  
remote\_function(:update =\> “ajaxmessage”, :url =\> {:action =\>  
‘update\_machine\_user’,  
:controller =\>  
‘manage\_machines’,:id=\>machine.id},:with=\>"‘users\_id=’+this.value")})  
%\>

Thanks William

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [September 9, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/collection-select-reseting-selection-whenever-page-refreshes/148071/2 "2008-09-09T12:29:12Z")

</div>

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001753)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.

I assume you have the Ajax and the update parts right. It is a  
question of populating the right value into @machine.

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [September 30, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/collection-select-reseting-selection-whenever-page-refreshes/148071/3 "2008-09-30T16:45:21Z")

</div>

Mukund wrote:

> [ActionView::Helpers::FormOptionsHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001753)
> 
> I assume you have the Ajax and the update parts right. It is a  
> question of populating the right value into @machine.

This is NOT working. See  
[http://rails.lighthouseapp.com/projects/8994/tickets/1138-collection\_select-doesnt-select-current-choice](http://rails.lighthouseapp.com/projects/8994/tickets/1138-collection_select-doesnt-select-current-choice)  
for details.  
Id is a FixNum value while the selected value is transfered as a String  
value.

So value and selected can never be the same in option\_value\_selected? If  
value is FixNum!
