0

I need to re-write this code below to work in rails format. I alread googled, unsuccessfully.

<select id="ddlSelect">
      <option>Jobs</option>
      <option>Products</option>
      <option>Other</option>
    </select>
<input type="text" id="hdnPro" value="product" style='display:none'/>


$('#ddlSelect').on('change',function(){
   if($(this).val() == 'Products'){
        $('#hdnPro').show();
      }else{
        $('#hdnPro').hide();
      }
});

I need to convert this code above to work inside a rails framework.

In the view I tried this way, /app/vies/gastos/_form.html.erb:

 <div align="center">
  <%= form_for(@gasto) do |f| %>


    <div class="form-group">
      <div class="field" id="ddlSelect">
        <%= f.label "Tipo de Pagamento" %><br>
        <%= f.select_tag(:payment_type, options_for_select([['À vista', 1], ['À prazo', 2], ['Misto', 3]]), { include_blank: true } ) %>   

      </div>

      <div class="field" id="hdnPro" style='display:none'>
        <%= render 'payment_flows/form' %>
      </div> 


    </div>
  <% end %>
</div>

And the javascript file this way, in the location /app/assets/javascript:

$('#ddlSelect').change(function(){
  $ajax({
    $('#ddlSelect').('change',function(){
        if($(this).val() == 'À prazo'){
            $('#hdnPro').show();
        }else{
            $('#hdnPro').hide();

    })
  })
});

Sorry for my bad english.

My Repository

Paulo
  • 33
  • 1
  • 6
  • Possible duplicate of [Rails 4: how to use $(document).ready() with turbo-links](http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links) – Vucko Sep 19 '16 at 11:30

2 Answers2

0
$('#ddlSelect').on('change',function(){
   if($(this).val() == '2'){
        $('#hdnPro').show();
   }else{
        $('#hdnPro').hide();
   }
});

Try with this JavaScript.

Pang
  • 9,564
  • 146
  • 81
  • 122
0

This is my solution:

  <div class="field" id="ddlSelect">
    <%= f.label "Tipo de Pagamento" %><br>
    <%= f.select_tag(:payment_type, options_for_select([['À vista', 1], ['À prazo', 'À prazo'], ['Misto', 3]]), include_blank: true, :onchange => 'teste()') %> 

  <div class="field" id="hdnPro" style='display:none'>
    <%= render 'payment_flows/form' %>
  </div> 

    <script >
      $('#payment_type').on('change',function(){
         if($(this).val() == 'À prazo'){
              $('#hdnPro').show();
         }else{
              $('#hdnPro').hide();
         }
      });
    </script>  


  </div>
Paulo
  • 33
  • 1
  • 6