title: How to use onload function in rails view?
body: <p>I'm want to load a function when the form is completely loaded. 
  This is my controller with the function which receive the ajax:</p>
  
  <pre><code>def get_component_from_work
    @work = Work.find(params[:work_id])
    @components=@work.components
    @work = Work.find(params[:work_id])
    @start = @work.start_date_of_work
    @finish = @work.real_end_date_of_work
    @entity = @work.entity.name
      contr = Entity.find(@work.contractor_id)
    @contractor = contr.name
    render json: {:start=&gt;@start, :finish=&gt;@finish, :entity=&gt;@entity, :contractor=&gt;@contractor, :component_work =&gt; @components}  
  end
  </code></pre>
  
  <p>This is my form:</p>
  
  <pre><code>.panel.panel-default
    .panel-heading
      %h4.panel-title
        %a{"data-parent" =&gt; "#accordion-ce", "data-toggle" =&gt; "collapse", href: "#collapse"+@reg.to_s+"-1"}
          %i.fa.fa-fw.fa-plus-circle.txt-color-green
          %i.fa.fa-fw.fa-minus-circle.txt-color-red
          Certificados
    .panel-collapse.collapse.in{ 'id'=&gt; "collapse"+@reg.to_s+"-1"}
      .panel-body
        .row
          .col-sm-4
            %label Nombre de la obra
            %select.form-control{'class'=&gt;'work-select', name: "professional[certificates_attributes][" + @reg.to_s + "][work_id]", onchange: 'workSelectCH(this);', onkeyup:'workSelectKU(this);'}
              %option
              -@work.each do |wc|
                %option{value: "#{wc.id}"} 
                  = "#{wc.name}"
            %input{'class'=&gt;'change-check' ,type: "checkbox", :value=&gt; "otros", onchange:'check(this);'}
              = "No es una de las Obras?"
          .col-sm-4{'class' =&gt;'another_work' ,style: "display:none;"}
            %label Nombre de Otra Obra
            %input.form-control{name: "professional[certificates_attributes][" + @reg.to_s + "][other_work_attributes][name]"}
          .col-sm-2
            %label Inicio de la obra: 
            %label.input{'class'=&gt;'start_other_works', 'style'=&gt;'display:none;'}
              %input.form-control{name:"professional[certificates_attributes][" + @reg.to_s + "][other_work_attributes][start]",  as: :date, type:  "date"}
            %label.input{'class'=&gt;'start_certificate'}
              %input.form-control{:disabled=&gt;"disabled", as: :date, type:  "date"}
          .col-sm-2
            %label Termino de la obra: 
            %label.input{'class'=&gt;'finish_other_works', 'style'=&gt;'display:none;'}
              %input.form-control{name: "professional[certificates_attributes][" + @reg.to_s + "][other_work_attributes][end]", as: :date, type:  "date"}
            %label.input{'class'=&gt;'finish_certificate'}
              %input.form-control{:disabled=&gt;"disabled", as: :date, type:  "date"}
          .col-sm-4{'class'=&gt;'specialty-select', style: "display:none;"}
            %label Especialidad
            %br
            .input-group
              %select.form-control{:name =&gt;"professional[certificates_attributes][" +@reg.to_s + "][other_work_attributes][specialty]"}
                %option
                %option{:value =&gt; "Saneamiento"} Saneamiento
                %option{:value =&gt; "Civil"} Civil
                %option{:value =&gt; "Electromecanico"} Electromecanico
                %option{:value =&gt; "Hidraulica"} Hidráulica
              %span.input-group-addon
                %i.fa.fa-money
        %br
        .row{'class'=&gt;'2'}
          .col-sm-4
            %label Entidad contratante
            .input-group{'class'=&gt;'entity_other_works', 'style'=&gt;'display:none;'}
              %input.form-control{name: "professional[certificates_attributes][" + @reg.to_s + "][other_work_attributes][entity]"}
              %span.input-group-addon
                %i.fa.fa-gears
            .input-group{'class'=&gt;'entity'}
              %input.form-control{:disabled=&gt;"disabled"}
              %span.input-group-addon
                %i.fa.fa-gears
          .col-sm-4
            %label Contratista
            .input-group{'class'=&gt;'contractor_other_works', 'style'=&gt;'display:none;'}
              %input.form-control{ name: "professional[certificates_attributes][" + @reg.to_s + "][other_work_attributes][contractor]"}
              %span.input-group-addon
                %i.fa.fa-gears
            .input-group{'class'=&gt;'contractor'}
              %input.form-control{:disabled=&gt;"disabled"}
              %span.input-group-addon
                %i.fa.fa-gears
  
          .col-sm-4
            %label Cargo
            %select.form-control#charge{name: "professional[certificates_attributes][" + @reg.to_s + "][charge_id]"}
              -@charge.each do |wc|
                %option{value: "#{wc.id}"} 
                  = "#{wc.name}"
        %br
        .row{'class'=&gt;'3'} 
          .col-sm-4
            %label Cantidad de días trabajados
            %input.form-control{'class'=&gt;'worked-day', name: "professional[certificates_attributes][" + @reg.to_s + "][num_days]", :disabled=&gt;"disabled"}
  
        %br
        .row
          .col-sm-2
            //%a.btn.btn-primary{href: 'javascript:void(0);', :onclick =&gt; 'add_dates(this);'} Agregar más fechas
  
        %br
        .row{'class'=&gt;'fechas'}
          .col-sm-4
            %label.col-sm-4 Fecha de Inicio:
            .col-sm-8
              .input-group
                %input.form-control{'class'=&gt;'start-worker', name: "professional[certificates_attributes][" + @reg.to_s + "][start_date]", as: :date, type:  "date"}
                %span.input-group-addon
                  %i.fa.fa-calendar
  
          .col-sm-4
            %label.col-sm-4.control-label Fecha de Termino:
            .col-sm-8
              .input-group
                %input.form-control{'class'=&gt;'finish-worker', name:"professional[certificates_attributes][" + @reg.to_s + "][finish_date]", as: :date, type:  "date", onchange:'calculateDays(this);'}
                %span.input-group-addon
                  %i.fa.fa-calendar
        %br
        .row
          .col-sm-12
            .new-date 
        .row{'class'=&gt;'comp2'}
          .col-sm-6
            %label Componentes de la obra:
            %br
            .col-sm-4
              %label#get-comp
                %label.select.components{'class'=&gt;'comp-check'}
        .row{'class'=&gt;'comp'}
          .col-sm-6
            .col-sm-4{'class'=&gt;'comp', style: "display:none;"}
              -if @component !=nil
                - @component.each do |comp|
                  %input{name: "professional[certificates_attributes][" + @reg.to_s + "][other_work_attributes][component_work]", type: "checkbox", :value=&gt; "#{comp.id}"}
                    = "#{comp.name}"
                  %br
        %fieldset
          %legend Archivos a Adjuntar
          .row
            .col-sm-6
              %label.col-sm-2 Certificado
              .col-sm-10
                .button
                  = file_field_tag "professional[certificates_attributes][" + @reg.to_s + "][certificate]", class: 'btn btn-default', label: false, onchange: "this.parentNode.nextSibling.value = this.value", as: "file"
            .col-sm-6
              %label.col-sm-2 Otro
              .col-sm-10
                .button
                  = file_field_tag "professional[certificates_attributes][" + @reg.to_s + "][other]", class: 'btn btn-default', label: false, onchange: "this.parentNode.nextSibling.value = this.value", as: "file"
  </code></pre>
  
  <p>When the page is completely load, some fields have to be completed with this js code using ajax:</p>
  
  <pre><code>function complete_infocombo(element, url, work_id, form_token, default_value){
  var str_html = "";
  $.ajax({
      type: "POST",
      url: url,
      async: false,
      data: { work_id: work_id, authenticity_token: form_token}
  }).done(function( data ) {
    var start = data.start;
    var finish = data.finish;
    var entity = data.entity;
    var contractor = data.contractor;
    newOptions = [];
    $.each(data.component_work, function(key, value){
      item = {};
      item['name'] = value.name;
      item['id'] = value.id;
      newOptions.push(item);
    });
    $(element).parent().parent().parent().children('.comp2.row').children('.col-sm-6').children('.col-sm-4').children('#get-comp').children('.comp-check.components.select').empty();
    $.each(newOptions, function(key,value){
      str_html = str_html + "&lt;label&gt; &lt;strong&gt; &gt; &lt;/strong&gt; " + value.name + "&lt;/label&gt;&lt;br/&gt;"
    });
    $(element).parent().parent().parent().children('.comp2.row').children('.col-sm-6').children('.col-sm-4').children('#get-comp').children('.comp-check.components.select').html(str_html);
    $(element).parent().parent().children().children('.start_certificate').find('input').empty();
    $(element).parent().parent().children().children('.start_certificate').find('input').val(start);
    $(element).parent().parent().children().children('.finish_certificate').find('input').empty();
    $(element).parent().parent().children().children('.finish_certificate').find('input').val(finish);
    $(element).parent().parent().parent().children('.2.row').children('.col-sm-4').children('.entity.input-group').find('input').empty();
    $(element).parent().parent().parent().children('.2.row').children('.col-sm-4').children('.entity.input-group').find('input').val(entity);
    $(element).parent().parent().parent().children('.2.row').children('.col-sm-4').children('.contractor.input-group').find('input').empty();
    $(element).parent().parent().parent().children('.2.row').children('.col-sm-4').children('.contractor.input-group').find('input').val(contractor);
      });
    }
  </code></pre>
  
  <p>I use the $(element) because I have a button which create a new certificate in the same form, after all is complete load I have to select one of the fields so the js works, but I want to the function work when the page is loaded and show the information in the field. I know that  html triggered the function onload and it works, is any way to do the same in rails?</p>