Forum: Ruby on Rails Access instance variable in ajax rendered by controller - Rails

Posted by nikhil rn (nikhilrkn)
on 2013-03-11 11:17
I am making an ajax call to my controller. Controller is sending in the
response in an instance variable(as a json object). How can I access the
same in my ajax javascript?

This is my ajax call code:

$(document).ready(function(){
var currentCellText;
$(".inline").click(function() {
currentCellText = $(this).text();
$.ajax({
type: 'GET',
dataType: "json",
url:'/test',
async: false,
data:{ foo1:currentCellText
},
dataType: "json",
success:function(data){
alert('<%= @response %>');
},
error:function(data){
alert("Error");
}
});
});
});

This is my controller code.

def some_action
# after assigning a value to variable response
@response = Response.where(some condition)
respond_to do |format|
format.html #this will simply render the view
format.json { render :json => { :response => @response } }
end
end

I am getting an empty square bracket as my output. I am not able to find
out where I am wrong. Please guide me through.
Posted by Robert Walker (robert4723)
on 2013-03-11 21:15
nikhil rn wrote in post #1101054:
> I am making an ajax call to my controller. Controller is sending in the
> response in an instance variable(as a json object). How can I access the
> same in my ajax javascript?
>
> $(document).ready(function(){
>   var currentCellText;
>   $(".inline").click(function() {
>     currentCellText = $(this).text();
>     $.ajax({
>       type: 'GET',
>       dataType: "json",
>       url:'/test',
>       async: false,
>       data:{ foo1:currentCellText
>       },
>       dataType: "json",
>       success:function(data){
>         alert('<%= @response %>');
>       },
>       error:function(data){
>         alert("Error");
>       }
>     });
>   });
> });
>
> I am getting an empty square bracket as my output. I am not able to find
> out where I am wrong. Please guide me through.

Have you confirmed for certain that your action method is returning the 
JSON you expect?

$ curl http://localhost:3000/test.json
Posted by nikhil rn (nikhilrkn)
on 2013-03-12 05:20
Hello Robert Walker.

After some operation in the controller, I am assigning a value to the
variable, response. When I print it in the controller using a "puts", I 
am getting
the right output. But when I render it to the view, I am NOT getting the
required json alert in the javascript. Where am I going wrong? Can you
please guide me?

Nikhil
Posted by nikhil rn (nikhilrkn)
on 2013-03-12 14:17
Am I trying to send it wrong from the controller? I mean, is my syntax 
wrong in sending the JSON from controller to the view? I have used such 
a JSON in other methods in the controller. I have no issues there. But I 
am not able to understand why I am not able to pass the same to the view 
using respond_to in controller.

Nikhil
Posted by nikhil rn (nikhilrkn)
on 2013-03-13 13:46
Now I am able to pass the required json properly. How do I alert the 
same in javascript.

Nikhil
Posted by "Сергей Соколов" <sokolov.sergey.a@gmail.com> (Guest)
on 2013-03-14 01:26
(Received via mailing list)
success:function(data){
alert(data);
}

2013/3/12   <sokolov.sergey.a@gmail.com>
Posted by nikhil rn (nikhilrkn)
on 2013-03-14 05:52
If I print the json object value from the controller, I am getting the 
required output. But if I alert the same in the ajax, my output is as 
below:

[object Object]

Nikhil
Posted by Dheeraj Kumar (Guest)
on 2013-03-14 06:10
(Received via mailing list)
alert(object) calls object.toString() which returns [object Object] for 
any object. If you want to dump the contents of the object, loop through 
it and print each key-value pair.

--
Dheeraj Kumar
Posted by nikhil rn (nikhilrkn)
on 2013-03-14 06:37
My json data printed at the controller is correct. But my data.length at 
the ajax javascript says "undefined". Even if i loop, it doesnt enter 
the loop as data.length is undefined.


success(data){
var x = new Array();
alert("created new array");
for (var i=0;i<data.length;i++)
{
x[i] ,x[data[i].toString()]=function(){};
}
}


Nikhil
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.