Passing javascript variables to rails controller

后端 未结 3 1616
予麋鹿
予麋鹿 2020-12-30 09:51

Here is the problem i\'m stuck on. I want to pass the javascript variables to the rails controller.



        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 10:24

    Ajax code in jQuery:

    $("#submit_button").submit(function(event) {
    
      /* stop form from submitting normally */
       event.preventDefault();
    
      /* get values from elements on the page: */
       var mdate = $('#mdate').val();
       var phone = $('#phone').val();
    
      /* Send the data using post and put the results in a div */
        $.ajax({
          url: "/BookCreate/?mdate="+mdate+"&phone="+phone,
          type: "post",
          data: values,
          success: function(){
            alert('Saved Successfully');
          },
          error:function(){
           alert('Error');
          }
        });
    });
    

    Routes : ( As I am assuming your controller name is book )

    match '/BookCreate', to: 'book#create'
    

    For this you have to add jquery file to your code or this link

    
    
    

    enter image description here

提交回复
热议问题