简体   繁体   English

Rails / Ajax / jQuery:将多个参数从jquery传递到Rails控制器

[英]Rails/Ajax/ jQuery: Pass multiple params from jquery to Rails controller

I need to pass two params (selected option & selected number of persons) from radio buttons through jquery to Rails controller via ajax and would appreciate any help at all on how to do so. 我需要将两个参数(选定的选项和选定的人数)从单选按钮通过jquery通过ajax传递给Rails控制器,并且将非常感谢您提供有关此操作的任何帮助。 Both params are needed to calculate pricing (please see @price code in controller below) and right now I'm only able to pass one param at a time for the calculation. 这两个参数都是计算价格所必需的(请参阅下面的控制器中的@price代码),现在我一次只能传递一个参数进行计算。

My form html code is shown below: 我的表单html代码如下所示:

    <%= form_for @cartedactivity, url: carted_activities_path, method: :post, remote: true do |f| %>
                    1. Choose option
                        <div class = "options">
                            <center> 
                                <% @options.each do |option| %> 
                                    <div class = "radio" id = "option_radio">
                                        <%= f.radio_button :supplier_activity_option_id, option.id, :onclick => "save_option();" %>
                                        <%= f.label :supplier_activity_option_id, option.option_name, :value => option.id %>
                                    </div>
                                <% end %> 
                            </center>
                        </div>


            <h5>2. Number of Persons</h5>
                <div class = "numbers">
                    <center>
                        <h5>
                            <%@numbers.each do |number| %>
                                <div class = "radio" id = 

"quantity_radio">
                                        <%= f.radio_button :activity_quantity, number, :onclick => "save_quantity();" %>
                                        <%= f.label :activity_quantity, number, :value => number %>
                                    </div>
                                <% end %> 
                            </h5>
                        </center>
                    </div>

                <h5>3. Start date
                <%= f.date_field :activity_date, min: (Date.today+2.days), max: (Date.today+4.months)%>
                </h5>

            <hr>

            <div id = "check-price-<%=@option%>"></div>


            <div class = "booking-btn">
                <%= f.submit "Add to Cart", class: "btn btn-book" %>
            </div>

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

<br><br>



<script> 

function save_option() 
    {var optionValue = $('input[name = "carted_activity[supplier_activity_option_id]"]:checked').val();
    if (optionValue) {
            $.ajax({
            type:    "POST",
            url:     "/supplier_activities/price_check",
            data:    { optionValue },
            success: function(post){ console.log('success') },
            error:   function(post){ console.log(this) }
          });
    }
};

function save_quantity() 
    {var quantityValue = $('input[name = "carted_activity[activity_quantity]"]:checked').val();
    if (quantityValue) {
        $.ajax({
            type:    "POST",
            url:     "/supplier_activities/price_check",
            data:    { quantityValue },
            success: function(post){ console.log('success') },
            error:   function(post){ console.log(this) }
          });
    }
};

The partial html is simply <% @price %> 部分html只是<%@price%>

The controller code is : 控制器代码为:

def price_check
    @option = params[:optionValue]
    @number = params[:quantityValue]
    if !@option.blank? && !@number.blank? 
        @price = Price.find_by("supplier_activity_option_id = ? AND min_qty_for_price <= ? AND max_qty_for_price >= ?", params[:optionValue], params[:quantityValue], params[:quantityValue]).price_usd 
    else 
        @price = 100
    end 
    respond_to do |format|
            format.js {render layout: false} 
    end
end 

Found the solution - have passed through the value of both checked radio buttons on each click of any radio button. 找到了解决方案-每次单击任何单选按钮时都要检查两个选中的单选按钮的值。 This means that whenever a radio button is clicked, the values of both checked buttons are passed in at the same time. 这意味着,每当单击单选按钮时,两个选中按钮的值都会同时传递。

    <script> 

        function save_option() 
    {var optionValue = $('input[name = "carted_activity[supplier_activity_option_id]"]:checked').val();
    var quantityValue = $('input[name = "carted_activity[activity_quantity]"]:checked').val();
    if (optionValue) {
        $.ajax({
          type:    "POST",
          url:     "/supplier_activities/price_check",
          data:    { optionValue: optionValue, quantityValue: quantityValue },
          success: function(post){ console.log('success') },
          error:   function(post){ console.log(this) }
        });
    }
  };

  function save_quantity() 
    {var quantityValue = $('input[name = "carted_activity[activity_quantity]"]:checked').val();
    var optionValue = $('input[name = "carted_activity[supplier_activity_option_id]"]:checked').val();
    if (quantityValue) {
      $.ajax({
          type:    "POST",
          url:     "/supplier_activities/price_check",
          data:    { quantityValue: quantityValue, optionValue: optionValue },
          success: function(post){ console.log('success') },
          error:   function(post){ console.log(this) }
        });
    }
  };

    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM