Forum: Ruby on Rails selectedIndex update

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
016e70ce625a3a44fc7f8b1d02ee9df1?d=identicon&s=25 Will (Guest)
on 2008-11-21 16:20
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-21 17:27
(Received via mailing list)
On Nov 21, 3:19 pm, Will <william_evan...@securecomputing.com> 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
016e70ce625a3a44fc7f8b1d02ee9df1?d=identicon&s=25 Will (Guest)
on 2008-11-23 00:03
(Received via mailing list)
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 <form id ="#{machine.fqdn}"> .... 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 Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-23 00:11
(Received via mailing list)
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 <form id ="#{machine.fqdn}"> .... 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
016e70ce625a3a44fc7f8b1d02ee9df1?d=identicon&s=25 Will (Guest)
on 2008-11-23 20:21
(Received via mailing list)
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 Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-23 20:48
(Received via mailing list)
On Nov 23, 7:20 pm, Will <william_evan...@securecomputing.com> 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
This topic is locked and can not be replied to.