nikhil rn
2013-Mar-11 10:17 UTC
Access instance variable in ajax rendered by controller - Rails
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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Robert Walker
2013-Mar-11 20:15 UTC
Re: Access instance variable in ajax rendered by controller - Rails
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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
nikhil rn
2013-Mar-12 04:20 UTC
Re: Access instance variable in ajax rendered by controller - Rails
Hello Robert Walker. After some operation in the controller, I am assigning a value to the variable. 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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
nikhil rn
2013-Mar-12 13:17 UTC
Re: Access instance variable in ajax rendered by controller - Rails
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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Сергей Соколов
2013-Mar-12 14:17 UTC
Re: Re: Access instance variable in ajax rendered by controller - Rails
success:function(data){ alert(data); } 2013/3/12 Сергей Соколов <sokolov.sergey.a@gmail.com>> Can you show the code that sends the variable to the controller? And code > that should render this variable. > > > 2013/3/12 nikhil rn <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > >> 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 via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
nikhil rn
2013-Mar-13 12:46 UTC
Re: Access instance variable in ajax rendered by controller - Rails
Now I am able to pass the required json properly. How do I alert the same in javascript. Nikhil -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
nikhil rn
2013-Mar-14 04:52 UTC
Re: Access instance variable in ajax rendered by controller - Rails
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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Dheeraj Kumar
2013-Mar-14 05:09 UTC
Re: Re: Access instance variable in ajax rendered by controller - Rails
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 On Thursday 14 March 2013 at 10:22 AM, nikhil rn wrote:> 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 via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org (mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org). > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
nikhil rn
2013-Mar-14 05:37 UTC
Re: Access instance variable in ajax rendered by controller - Rails
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.