jquery - How to pass html values in Ruby within a Javascript function -
i have view page, user inputs number , clicks on generate form in same page based on input. i'm using javascript in main view:
<div class="label-field-pair"><%= "#{t('total')} #{t('amount')}" %>: <%= form.text_field :total, :value =>precision_label(@total_pay.to_f) , :disabled=> true , :id=>'total' %></div> <div class="label-field-pair"> <label for="student_grade"><%= t('no_payments') %><span class="necessary-field">*</span> </label> <input type="number" min="1" max="12" step="1" value="1" name="no_payments" id="no_payments"> </div> <%= form.hidden_field :no_payments, :id=>'payments' %> <%= form.hidden_field :total_pay, :id=>'total_pay' %> <%#= submit_tag "", :value => "► #{t('generate')}", :class => "submit_button", :disable_with => "► #{t('please_wait')}" %> <%#= render :partial => 'distribute_payments', :locals => {:no_payments=>2, :total => 30000 , :guardian=>@guardian.id} %> <button type="button" id="gen" onclick="generate()" style="display: block">generate</button> <div id="payments_distribute"></div> <%end%> <script type="text/javascript"> function generate() { var t = document.getelementbyid("total").value; var n = document.getelementbyid("no_payments").value; j('#total_pay').val("<%= @total_pay %>"); document.getelementbyid("gen").style.display = "none"; <%="#{remote_function(:url => {:action => "distribute_payments", :id=>@guardian.id,:total_pay=>@total_pay} )}"%> } </script>
and action partial view:
def distribute_payments @total_amount=params[:total] @no_payments=params[:no_payments] @total_payment=params[:total_pay] @guardian = guardian.find(params[:id]) @students=student.find_all_by_sibling_id(@guardian.ward_id) render(:update) |page| page.replace_html 'payments_distribute', :partial=>'distribute_payments', :total_pay=>@total_payment, :no_payments=>@no_payments end end
the problem here need pass value of "no_payments" parameters partial view, , don't know how javascript function (remote_function). trying use render_partial submit button, wasn't able open partial view in same page. can check question here: rails: render partial controller
you need use ajax:
<script type="text/javascript"> function generate() { var t = document.getelementbyid("total").value; var n = document.getelementbyid("no_payments").value; var id="<%= @guardian.id %>" j('#total_pay').val("<%= @total_pay %>"); document.getelementbyid("gen").style.display = "none"; j.ajax({ type: 'post' , url: '<%= url_for :action => "distribute_payments" %>', data : { 'no_payments' :n, 'total_payment' :t, 'id' :id }, beforesend : function() { }, success : function() { j("#payments_distribute").html(data) }}); } </script>
Comments
Post a Comment