Reputation: 211
I'm sorry this is a broad question. I need some guidance though.
I have this AJAX call:
$('#testyo').click(function(){
$.ajax({
type: 'GET',
url: "../messages/drew",
success:function(data){
alert(data);
}
});
return false;
});
which calls this rails method
def show
username = params[:id]
@client = Twilio::REST::Client.new Rails.application.config.account_sid, Rails.application.config.auth_token
@account = @client.account
received_messages = @account.messages.list
end
it takes the params "drew", just as a test, but I don't know how to pass data back to the ajax call. the alert(data) isnt showing anything. I'm I missing something somewhere?
Upvotes: 1
Views: 597
Reputation: 76774
MIME Types
As mentioned, you'll be best using JSON for this, however, let me explain the way in which Rails will pass your data back
When you pass a request to any web application, your server / framework will have to determine the mime type of that request. Mime types basically determine the type of request, whether it be a pure HTML one, JS one, etc.
Sending an Ajax request is essentially the same as sending an "HTTP" request, except that the ajax variant will have a different mime type. This allows you to adapt the functionality of your app to suit this (specifically with respond_to
):
#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
respond_to :js, :json, :html, only: :show
def show
username = params[:id]
@client = Twilio::REST::Client.new Rails.application.config.account_sid, Rails.application.config.auth_token
@account = @client.account
respond_with @account.messages.list
end
end
This will send the relevant data back to the mime type you initiate the request with. To do this, you have to ensure you're sending the request you want, allowing you to process the response as you need:
#app/assets/javascripts/application.js
$(document).on("click", "#testyo", function(){
$.ajax({
type: 'GET',
dataType: "JSON",
url: "../messages/drew",
success:function(data){
alert(data);
}
});
return false;
});
Ajax
You have to remember how Ajax handles requests / returned data.
Its known as Asynchronous Javascript and XML - meaning it's intended to be an asynchronous technology.
This means that if you send a request via Ajax, the idea is that you'll receive a "portion" / "snippet" of data, which you can then append to your page. Of course, this is dependent on your application's structure, but the bottom line is that if you want to receive data back from your app, you'll have to design the ajax call around the data you want returned
Upvotes: 2
Reputation: 7366
respond_to do |format|
format.json { render :json => @account.messages.list, :status => 200 }
end
in your ajax call
$('#testyo').click(function(){
$.ajax({
type: 'GET',
url: "../messages/drew",
dataType: 'json'
success:function(data){
alert(data);
}
});
return false;
});
Upvotes: 0
Reputation: 3721
try adding this in the end of your show.
def show
username = params[:id]
@client = Twilio::REST::Client.new Rails.application.config.account_sid, Rails.application.config.auth_token
@account = @client.account
respond_to do |format|
format.html # show.html.erb AND if and only if you also want view of show
format.json { render :json => @account.messages.list }
end
end
Upvotes: 1