Elsie Hamilton
Elsie Hamilton

Reputation: 155

How to make an ajax request in ruby on rails?

Hello I am new to ruby on rails I have just started to learn. I would like to know how can I make the ajax request. My js code:

$(document).ready(function(){
  $(".form_submit").click(function() {
         var apiid = $('#apiid').val();
         var apikey = $('#apikey').val();
         var email = $('#email').val();
           console.log("apiid  = ",apiid , " apikey = ",apikey , " email = ",email);

  });  
});

Now I want to send apiid , apikey and email to my controller.

def api 
//here I want apiid, apikey,email from js
end

Upvotes: 2

Views: 8439

Answers (4)

Codebeef
Codebeef

Reputation: 44036

Once you've sent the data to your server via js with something like

$.post("/your/url",
{
    apiid: "1",
    apikey: "1",
    email: "[email protected]"
});

Then you can access these parameters in your controller via the params hash:

def api
  params[:apiid]
  params[:apikey]
  params[:email]
end

Upvotes: 4

Jagdeep Singh
Jagdeep Singh

Reputation: 4920

You can use jQuery ajax option data to send the params in an ajax request:

$(document).ready(function(){
  $(".form_submit").click(function() {
    var apiid = $('#apiid').val();
    var apikey = $('#apikey').val();
    var email = $('#email').val();

    $.ajax(path_to_api_action, {
      data: { apiid: apiid, apikey: apikey, email: email }
      // Add other options
    })
  });  
});

Upvotes: 1

rohin-arka
rohin-arka

Reputation: 799

You can set apiid, apikey and email in hidden field or can assign in the formData properties later. All data here is grab from form. the url is the form action, method is an action set in form.

Note: .form_submit is a class of form not button.

$( '.form_submit' ).submit(function(e){
    e.preventDefault();
    var remoteCall = new RemoteCall(this);
    remoteCall.ajax()
  })
function RemoteCall(element){
  this.url = element.action;
  this.method = element.method;
  this.dataType = 'json';
  this.formData = $(element).serialize()
}
RemoteCall.prototype.ajax = function(){
    $.ajax({ method: this.method,
             url: this.url,
             data: this.formData,
             dataType: 'json'
          }).done(function(msg){
             alert('done'); 
          }).fail(function(msg){ alert('Sorry request could not complete'); });
}

Hope this will solve your problem.

Upvotes: 1

Miguel Coder
Miguel Coder

Reputation: 1949

Assuming that you already have you RESTful API setup on your Rails Server, then all you would have to do is send the Ajax request. If you haven't done that, this tutorial will teach you how to do it.

https://www.codementor.io/ruby-on-rails/tutorial/creating-simple-api-with-rails

After that all you have to do is:

$.post("/path/to/url",
{
    apiid: "1",
    apikey: "1",
    email: "[email protected]"
},
function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
});

The first parameter is the route endpoint, the second parameter is your json data, and the third parameter is a callback function that is executed when the server is done executing it's update.

Upvotes: 1

Related Questions