Topic: Collection_Select - Jquery

Hi,

I'm using Rails 3.1 and  I have a collection_select dropdown box that displays a client name:

<%= collection_select(:user_client, :client_id, UserClient.order('client_id'), :client_id, :client_name, {:selected => current_client.client_id},{}  ) %>

What I need to happen with I'm assuming Jquery is when the combo box is changed I need whatever client is selected to update a table in my database (user_clients)  that this is now the default client. I'm new to Jquery and stuck. Any help would be appreciated.

Pete

Re: Collection_Select - Jquery

That second empty hash is for html options, if you put

:onchange=>'yourjs'

in that hash,  you'll execute a javascript function called 'yourjs'.

so now you'd need to define that function,  you can do that directly in your view, or put it in a separate .js file.  In your view it would look like:

<head>
  <script type="text/javascript">
    $(document).ready(function() {
    });

    yourjs = function() {
    }  
  </script>
</head>

Whatever code you put inside the $(document).ready function will execute once that page is loaded.  The yourjs is just a function that will be there, waiting until you call it.

Now there is another approach,  instead of :onchange=>'yourjs',  you could put :id=>'yourselectorid'.   That will set the id of the HTML select element to 'yourselectorid'.  Then you could do this:

<head>
  <script type="text/javascript">
    $(document).ready(function() {
       $('#yourselectorid').bind('change', function() {
       //  don't know yet
       });
    }); 
  </script>
</head>

This second approach is now preferred,  as it keeps the javascript out of the HTML.  SO now what?  Well you probably want to use AJAX to set the client to the default.  So the first thing you need is a url that you can call that will set the client to be the default client.   Have you done that?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.