Guest User

Untitled

a guest
Dec 10th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. ## _form.html.erb
  2. #
  3. <script type="text/javascript">
  4. jQuery(function($) {
  5. // when the #country field changes
  6. $("#provincia_id").change(function() {
  7. // make a POST call and replace the content
  8. var provincia = $('select#provincia_id :selected').val();
  9. if(provincia == "") provincia="0";
  10. jQuery.get('/users/update_departamento_select/' + provincia, function(data){
  11. $("#direccionDepartamentos").html(data);
  12. })
  13. return false;
  14. });
  15.  
  16. })
  17. </script>
  18.  
  19. <%= form_for(@user, :remote => true) do |f| %>
  20.  
  21. <li><label>Provincia: </label>
  22. <%= select 'provincia', 'id', @provincias.map {|provincia| [provincia.descripcion, provincia.id]} , { :include_blank => true } %></li>
  23. <div id="direccionDepartamentos">
  24. <%= render :partial => 'partials/departamentos' %>
  25. </div>
  26. <div id="direccionLocalidades">
  27. <%= render :partial => 'partials/localidades' %>
  28. </div>
  29.  
  30. </ul>
  31. <p align="center"><%= f.submit 'Enviar', :class=>"btn" %></p>
  32. </fieldset>
  33. <% end %>
  34.  
  35. ## users_controller.rb
  36. #
  37. def update_departamento_select
  38. @departamentos = Departamento.where(:provincia_id=>params[:id]).order(:descripcion) unless params[:id].blank?
  39. render :partial => "partials/departamentos"
  40. end
  41.  
  42. def update_localidad_select
  43. @localidades = Localidad.where(:departamento_id=>params[:id]).order(:descripcion) unless params[:id].blank?
  44. render :partial => "partials/localidades"
  45. end
  46.  
  47. ## _departamento.html.erb
  48. #
  49. <script type="text/javascript">
  50. jQuery(function($) {
  51. $("#departamento_id").change(function() {
  52. var departamento = $('select#departamento_id :selected').val();
  53. if(departamento == "") departamento="0";
  54. jQuery.get('/users/update_localidad_select/' + departamento, function(data){
  55. $("#direccionLocalidades").html(data);
  56. })
  57. return false;
  58. });
  59. })
  60. </script>
  61. <% if !@departamentos.blank? %>
  62. <li><label>Departamento: </label>
  63. <%= select 'departamento', 'id', @departamentos.map {|dep| [dep.descripcion, dep.id] }, { :include_blank => true }, :style => 'width:200px' %></li>
  64. <% else %>
  65. <li><label>Departamento: </label>|</li>
  66. <% end %>
  67.  
  68. ## _localidades.html.erb
  69. #
  70. <% if !@localidades.blank? %>
  71. <li><label>Localidad: </label>
  72. <%= select 'localidad', 'id', @localidades.map {|loc| [loc.descripcion, loc.id] }, { :include_blank => true }, :style => 'width:200px' %></li>
  73. <% else %>
  74. <li><label>Localidad: </label>|</li>
  75. <% end %>
  76.  
  77. ## routes.rb
  78. #
  79. resources :users do
  80. resources :asistencias
  81. end
  82. match 'users/update_departamento_select/:id', :controller=>'users',
  83. :action => 'update_departamento_select'
  84. match 'users/update_localidad_select/:id', :controller=>'users', :action
  85. => 'update_localidad_select'
Add Comment
Please, Sign In to add comment