Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/04/2012, 13:24
patricia:)
 
Fecha de Ingreso: abril-2006
Ubicación: Venezuela
Mensajes: 126
Antigüedad: 17 años, 11 meses
Puntos: 0
Combos dependiente en Ruby

Hola a todos,

Estoy usando ruby 1.8.7 y rails 2.3.14, estoy intentando crear 2 combos donde el 2do depende de lo que se seleccione en el primero.

El 1ro lee de la tabla proyecto y el 2do busca las fases de ese proyecto seleccionado y esas 2 selecciones debe insertarse en la tabla activifases.

Intente utilizar la libreria de javascript http://ajax.googleapis.com/ajax/libs.../jquery.min.js y no me funciono.

En el controlador de Fases cree el siguiente metodo
Código:
def for_proyectoid 
    @fases = Fase.find_all_by_proyecto_id(params[:id])
    #Notifier.deliver_gmail_message('[email protected]', @fases.descripcion, 'enviando correo de prueba')
    respond_to do | format |
        format.json  { render :json => @fases }
    end
  end
en el new de activifases coloque lo siguiente:
Código:
<script type="text/javascript">
    $(document).ready(function(){
        $("select#activifase_proyecto_id").change(function(){
            var id_value_string = $(this).val();
            alert(id_value_string);
            if (id_value_string == "") { 
                // if the id is empty remove all the fases options from being selectable and do not do any ajax
                $("select#activifase_fase_id option").remove();
                var row = "<option value=\"" + "" + "\">" + "" + "</option>";
                $(row).appendTo("select#activifase_fase_id");
                
            }
            else {
                // Send the request and update sub category dropdown 
                $.ajax({
                    dataType: "json",
                    cache: false,
                    url: '/fases/for_proyectoid/' + id_value_string,
                    timeout: 5000,
                    error: function(XMLHttpRequest, errorTextStatus, error){
                        alert("Failed to submit : "+ errorTextStatus+" ;"+error);
                    },
                    success: function(data){                           
                        // Clear all options from sub category select 
                        //put in a empty default line
                        var row = "<option value=\"" + "" + "\">" + "" + "</option>";
                        $(row).appendTo("select#activifase_fase_id"); 
                        alert( $("select#activifase_fase_id"));                        
                        // Fill sub category select                         
                        $.each(data, function(i, j){
                            row = "<option value=\"" + j.fases.id + "\">" + j.fases.descripcion + "</option>";                             
                            $(row).appendTo("select#activifase_fase_id");   
                                                     
                        });             
                     }
                });
            };
                });
    });
</script>
<% form_for @activifase do |f| %>
  <p>
    <%= f.label :proyecto_id %>
    <%= f.collection_select :proyecto_id, Proyecto.find_by_sql("select c.* from jerarquias x, usuarios b ,jerarquias_proyectos d , proyectos c where x.empleado_id = b.id and   nvl(x.fecha_hasta, sysdate)>=sysdate and   d.supervisor_id = x.supervisor_id and   d.empleado_id   = x.empleado_id and   to_number(c.creado_por) = x.supervisor_id and   nvl(c.fecha_hasta,sysdate)>=sysdate and   c.id = d.proyecto_id and   b.id = #{@user.id}"), :id, :descripcion, :prompt => "Seleccione un Proyecto" %>
  </p>
  <p >
    <%= f.label :fase_id %>
   <%= f.collection_select :fase_id, Fase.find_all_by_proyecto_id(@activifase.proyecto_id), :id, :descripcion, :prompt => '' %>
  </p>  
<% end %>
Probe y el 2do combo no se esta llenando, pense que el procedimiento for_proyectoid (controlador fases) no estaba funcionando sin embargo al correrlo desde el explorer " http://localhost:3000/fases/for_proyectoid/10120 " me retorna lo siguiente
Código:
[{"fase":{"proyecto_id":10120,"created_at":"2012-04-06T22:36:04Z","updated_at":"2012-04-06T22:36:04Z","descripcion":"Analisis","id":10100,"fecha_desde":"2012-04-06T00:00:00Z","tiempo_estimado":null,"fecha_hasta":"2012-04-17T00:00:00Z","status":null}},{"fase":{"proyecto_id":10120,"created_at":"2012-04-06T22:36:04Z","updated_at":"2012-04-06T22:36:04Z","descripcion":"Dise\u00f1o ","id":10101,"fecha_desde":"2012-04-18T00:00:00Z","tiempo_estimado":null,"fecha_hasta":"2012-04-26T00:00:00Z","status":null}},{"fase":{"proyecto_id":10120,"created_at":"2012-04-06T22:36:04Z","updated_at":"2012-04-06T22:36:04Z","descripcion":"Implementacion","id":10102,"fecha_desde":"2012-04-26T00:00:00Z","tiempo_estimado":null,"fecha_hasta":"2012-05-06T00:00:00Z","status":null}},{"fase":{"proyecto_id":10120,"created_at":"2012-04-07T17:46:30Z","updated_at":"2012-04-07T17:46:30Z","descripcion":null,"id":10120,"fecha_desde":"2012-04-07T00:00:00Z","tiempo_estimado":null,"fecha_hasta":"2012-04-07T00:00:00Z","status":null}}]
He revisado iles de veces y ya no se que hacer, por favor si me pueden ayudar, se los agradecere infinitamente.

Saludos,
Patricia