# selectedIndex update

**URL:** <https://www.ruby-forum.com/t/selectedindex-update/153831>\
**Category:** Rails\
**Created:** [November 21, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/selectedindex-update/153831 "2008-11-21T16:20:20Z")\
**Posts on this page:** 6\
**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:** [November 21, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/selectedindex-update/153831/1 "2008-11-21T16:20:20Z")

</div>

I have a table with a collection select object that I am looking  
update the selectedIndex of using links on the page

\<%= collection\_select(:machine, :user\_id, @users, :id, :fullname,  
{: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")})  
%\>

Currently the links look like:  
]\<% if @machine.user\_id != session[:user\_id] %\>  
\<%= link\_to(image\_tag(‘user\_add.png’), {:action =\>  
‘update\_machine\_user’, :id =\> @machine, :users\_id=\>session[:user\_id]},  
{:border =\> 0, :title =\> “Edit Details”, :class =\> “image\_link”}) %\>  
\<%else%\>  
\<%= link\_to(image\_tag(‘user\_reset.png’), {:action =\>  
‘update\_machine\_user’, :id =\> @machine, :users\_id=\>nil}, {:border =\>  
0, :title =\> “Edit Details”, :class =\> “image\_link”}) %\>  
\<%end%\>

Although they work they do a screen refresh of course since its not

Here is the controller used.

def update\_machine\_user  
@machine = Machine.find(params[:id])  
@machine.user\_id = params[:users\_id]  
#flash.keep[:notice] = “User ID #{params[:user\_id]}”  
@machine.save  
#flash[:notice] = ‘User updated!!!’  
redirect\_to session[:last\_list]  
end

Can anyone give me some ideas of how to ajaxify this to update the DOM  
element.

Thanks

---

<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:** [November 21, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/selectedindex-update/153831/2 "2008-11-21T17:27:11Z")

</div>

On Nov 21, 3:19 pm, Will [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a table with a collection select object that I am looking  
> update the selectedIndex of using links on the page

Does it need to be any more complicated than $  
(‘machine\_user\_id’).selectedIndex = whatever (check that I got the  
dom\_id of your select box right ?)  
if you also want to change the images on the links you’ll probably  
make life easier for your self if the image for the link is a css  
property.

Fred

---

<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:** [November 23, 2008, 12:03am UTC](https://www.ruby-forum.com/t/selectedindex-update/153831/3 "2008-11-23T00:03:32Z")

</div>

The problem is that is the collection select object is repeated in a  
table of objects so this ID appears multiple times. To deal with it I  
created a … collection\_select object \</  
form\> around each of these and tried to refer to them using $(“#  
{machine.fqdn}”).machine\_user\_id.selectedIndex=whatever, but the  
prototype $() function does not seem to want to take a variable. It  
seems to work with a static id, but not a variable one. Any ideas how  
this could be done?

Thanks  
Will

On Nov 21, 10:26 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [November 23, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/selectedindex-update/153831/4 "2008-11-23T20:21:23Z")

</div>

Is there a way to tell collection\_select to use a specific ID then  
other than machine\_user\_id. That is what I was originally trying to  
do so I did not have to go down into the object from the form .  
I was trying to get around it by using a call to a form $(“#  
{machine.fqdnj}”).machine[user\_id].selectedIndex= and it did not seem  
to work. When I was trying this with a standard html setup with  
prototype I found that it would not accept a variable being passed  
into $() but if I set the name using a static name it seemed to work.

On Nov 22, 5:10 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [November 23, 2008, 12:11am UTC](https://www.ruby-forum.com/t/selectedindex-update/153831/5 "2008-11-23T00:11:10Z")

</div>

On 22 Nov 2008, at 23:02, Will wrote:

> The problem is that is the collection select object is repeated in a  
> table of objects so this ID appears multiple times. To deal with it I  
> created a … collection\_select object  
> \</  
> form\> around each of these and tried to refer to them using $("#  
> {machine.fqdn}").machine\_user\_id.selectedIndex=whatever, but the  
> prototype $() function does not seem to want to take a variable. It  
> seems to work with a static id, but not a variable one. Any ideas how  
> this could be done?

Having multiple elements on a page with the same dom id is a super  
super bad idea. Instead of creating that form with that special id,  
why not give that id to the select box ?  
The $ function is just a regular function - it doesn’t care whether  
its argument is a literal, a function call or something else.  
Regardless, ‘.machine\_user\_id’ is not how you would find the child of  
a certain element. If you want the child of an element matching a  
given css selector use the down function.

Fred

---

<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:** [November 23, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/selectedindex-update/153831/6 "2008-11-23T20:48:28Z")

</div>

On Nov 23, 7:20 pm, Will [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to tell collection\_select to use a specific ID then  
> other than machine\_user\_id. That is what I was originally trying to  
> do so I did not have to go down into the object from the form .  
> Of course!

collection\_select(:machine, :user\_id, @users, :id, :fullname,  
{:include\_blank =\> true}, {:id =\> ‘foo’})  
It’s just an html option
