I am hoping someone can please help me with this. I am trying to pass an array using Ajax.Request and cannot get it working. This is what I have so far: @data is a ruby array new Ajax.Request( ''/users/test'', { parameters: { data: Object.toJSON(<%array_or_string_for_javascript(@data) %>)}, asynchronous:true }); }) Does anyone have any ideas? Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, Jan 15, 2009 at 8:05 AM, David <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am hoping someone can please help me with this. I am trying to pass > an array using Ajax.Request and cannot get it working. This is what I > have so far: > > @data is a ruby array > > new Ajax.Request( ''/users/test'', { > parameters: { data: Object.toJSON(<%> array_or_string_for_javascript(@data) %>)}, asynchronous:true }); }) > > Does anyone have any ideas?new Ajax.Request( ''/users/test'', { parameters: { data: <%= @data.to_json %>}, asynchronous:true }); }) Rails adds a #to_json method to all of the built-in classes. -- Brandon -------------------------------------------------------------------------------- Training by Collective Idea: Refer a friend and get $100 http://training.collectiveidea.com – San Antonio, TX – Jan 20-23 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I tried this, but I still get an error: undefined method `toJSON'' for []:Array On Jan 15, 5:44 am, "Brandon Keepers" <bran...-NYlJC1mBhkRg9hUCZPvPmw@public.gmane.org> wrote:> On Thu, Jan 15, 2009 at 8:05 AM, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am hoping someone can please help me with this. I am trying to pass > > an array using Ajax.Request and cannot get it working. This is what I > > have so far: > > > @data is a ruby array > > > new Ajax.Request( ''/users/test'', { > > parameters: { data: Object.toJSON(<%> > array_or_string_for_javascript(@data) %>)}, asynchronous:true }); }) > > > Does anyone have any ideas? > > new Ajax.Request( ''/users/test'', { > parameters: { data: <%= @data.to_json %>}, asynchronous:true }); }) > > Rails adds a #to_json method to all of the built-in classes. > > -- Brandon > -------------------------------------------------------------------------------- > Training by Collective Idea: Refer a friend and get $100http://training.collectiveidea.com– San Antonio, TX – Jan 20-23--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nevermind, Its seems to be working now. This is what shows up in the console: "data"=>"[object Object]" and I am wondering, do I have to convert this json array back to ruby to use it and if so how do I do that? On Jan 15, 3:56 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I tried this, but I still get an error: > > undefined method `toJSON'' for []:Array > > On Jan 15, 5:44 am, "Brandon Keepers" <bran...-NYlJC1mBhkRg9hUCZPvPmw@public.gmane.org> wrote: > > > On Thu, Jan 15, 2009 at 8:05 AM, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I am hoping someone can please help me with this. I am trying to pass > > > an array using Ajax.Request and cannot get it working. This is what I > > > have so far: > > > > @data is a ruby array > > > > new Ajax.Request( ''/users/test'', { > > > parameters: { data: Object.toJSON(<%> > > array_or_string_for_javascript(@data) %>)}, asynchronous:true }); }) > > > > Does anyone have any ideas? > > > new Ajax.Request( ''/users/test'', { > > parameters: { data: <%= @data.to_json %>}, asynchronous:true }); }) > > > Rails adds a #to_json method to all of the built-in classes. > > > -- Brandon > > -------------------------------------------------------------------------------- > > Training by Collective Idea: Refer a friend and get $100http://training.collectiveidea.com–San Antonio, TX – Jan 20-23--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It seems like there is a better way to pass an array with Ajax.request into a controller and have it interpreted into a ruby array. Using link_to_remote, you are able to pass an array using :with -> <%= link_to_remote ''Click me'', :url => {:action => "array"}, :with => "''data=''+test()", :update => ''testing''%> Im wondering, isnt there a way to do this using Ajax.Request? On Jan 15, 3:59 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Nevermind, Its seems to be working now. This is what shows up in the > console: > > "data"=>"[object Object]" > > and I am wondering, do I have to convert this json array back to ruby > to use it and if so how do I do that? > > On Jan 15, 3:56 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I tried this, but I still get an error: > > > undefined method `toJSON'' for []:Array > > > On Jan 15, 5:44 am, "Brandon Keepers" <bran...-NYlJC1mBhkRg9hUCZPvPmw@public.gmane.org> wrote: > > > > On Thu, Jan 15, 2009 at 8:05 AM, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am hoping someone can please help me with this. I am trying to pass > > > > an array using Ajax.Request and cannot get it working. This is what I > > > > have so far: > > > > > @data is a ruby array > > > > > new Ajax.Request( ''/users/test'', { > > > > parameters: { data: Object.toJSON(<%> > > > array_or_string_for_javascript(@data) %>)}, asynchronous:true }); }) > > > > > Does anyone have any ideas? > > > > new Ajax.Request( ''/users/test'', { > > > parameters: { data: <%= @data.to_json %>}, asynchronous:true }); }) > > > > Rails adds a #to_json method to all of the built-in classes. > > > > -- Brandon > > > -------------------------------------------------------------------------------- > > > Training by Collective Idea: Refer a friend and get $100http://training.collectiveidea.com–San Antonio, TX – Jan 20-23--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So Ive decided it might be better to take a step backwards and explain what I am trying to do to because there may be a better way of doing it. I have an array of hashes and each hash correlates to an active record object. Now what I need to do is pass this array to a controller action using Ajax.Request somehow. I could really use some help, Ive tried many different ways but none have worked! Thanks. On Jan 15, 4:18 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It seems like there is a better way to pass an array with Ajax.request > into a controller and have it interpreted into a ruby array. Using > link_to_remote, you are able to pass an array using :with -> > > <%= link_to_remote ''Click me'', :url => {:action => "array"}, :with => > "''data=''+test()", :update => ''testing''%> > > Im wondering, isnt there a way to do this using Ajax.Request? > > On Jan 15, 3:59 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Nevermind, Its seems to be working now. This is what shows up in the > > console: > > > "data"=>"[object Object]" > > > and I am wondering, do I have to convert this json array back to ruby > > to use it and if so how do I do that? > > > On Jan 15, 3:56 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I tried this, but I still get an error: > > > > undefined method `toJSON'' for []:Array > > > > On Jan 15, 5:44 am, "Brandon Keepers" <bran...-NYlJC1mBhkRg9hUCZPvPmw@public.gmane.org> wrote: > > > > > On Thu, Jan 15, 2009 at 8:05 AM, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I am hoping someone can please help me with this. I am trying to pass > > > > > an array using Ajax.Request and cannot get it working. This is what I > > > > > have so far: > > > > > > @data is a ruby array > > > > > > new Ajax.Request( ''/users/test'', { > > > > > parameters: { data: Object.toJSON(<%> > > > > array_or_string_for_javascript(@data) %>)}, asynchronous:true }); }) > > > > > > Does anyone have any ideas? > > > > > new Ajax.Request( ''/users/test'', { > > > > parameters: { data: <%= @data.to_json %>}, asynchronous:true }); }) > > > > > Rails adds a #to_json method to all of the built-in classes. > > > > > -- Brandon > > > > -------------------------------------------------------------------------------- > > > > Training by Collective Idea: Refer a friend and get $100http://training.collectiveidea.com–San Antonio, TX – Jan 20-23--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi David, That depends on what you need the AR objects for, if they are records that already exist in your database and your controller only need the reference to them, you''ld be better off passing just the ids (and type if they differ) and having your controller action fetch the records. If the data in the active records has changed, or isn''t yet in the database, I''d have to recommend either using serializing them into a form to submit to the controller action or turning them into json as mentioned above. The response from your test of #to_json you showed before "data"=>"[object Object]" doesn''t seem right, as the result should be a string representation of all object''s in the array (and all object attributes & values) experiment with it in the console, to get it working, try data.to_json should work just fine, showing something like: "[{\"object_type\": {\"name\": \"Hi\", \"updated_at\": \"2009-01-16T22:59:51Z\", \"id\": 1, \"summary_text\": \"awesome!\", \"game_id\": 1, \"created_at\": \"2009-01-16T22:59:51Z\"}}]" where object_type is the name of the AR model, and all the fields and their values are shown (this example has only one object in the array). On Jan 16, 4:14 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So Ive decided it might be better to take a step backwards and explain > what I am trying to do to because there may be a better way of doing > it. I have an array of hashes and each hash correlates to an active > record object. Now what I need to do is pass this array to a > controller action using Ajax.Request somehow. I could really use some > help, Ive tried many different ways but none have worked! Thanks. > > On Jan 15, 4:18 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > It seems like there is a better way to pass an array with Ajax.request > > into a controller and have it interpreted into a ruby array. Using > > link_to_remote, you are able to pass an array using :with -> > > > <%= link_to_remote ''Click me'', :url => {:action => "array"}, :with => > > "''data=''+test()", :update => ''testing''%> > > > Im wondering, isnt there a way to do this using Ajax.Request? > > > On Jan 15, 3:59 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Nevermind, Its seems to be working now. This is what shows up in the > > > console: > > > > "data"=>"[object Object]" > > > > and I am wondering, do I have to convert this json array back to ruby > > > to use it and if so how do I do that? > > > > On Jan 15, 3:56 pm, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I tried this, but I still get an error: > > > > > undefined method `toJSON'' for []:Array > > > > > On Jan 15, 5:44 am, "Brandon Keepers" <bran...-NYlJC1mBhkRg9hUCZPvPmw@public.gmane.org> wrote: > > > > > > On Thu, Jan 15, 2009 at 8:05 AM, David <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I am hoping someone can please help me with this. I am trying to pass > > > > > > an array using Ajax.Request and cannot get it working. This is what I > > > > > > have so far: > > > > > > > @data is a ruby array > > > > > > > new Ajax.Request( ''/users/test'', { > > > > > > parameters: { data: Object.toJSON(<%> > > > > > array_or_string_for_javascript(@data) %>)}, asynchronous:true }); }) > > > > > > > Does anyone have any ideas? > > > > > > new Ajax.Request( ''/users/test'', { > > > > > parameters: { data: <%= @data.to_json %>}, asynchronous:true }); }) > > > > > > Rails adds a #to_json method to all of the built-in classes. > > > > > > -- Brandon > > > > > -------------------------------------------------------------------------------- > > > > > Training by Collective Idea: Refer a friend and get $100http://training.collectiveidea.com–San Antonio, TX – Jan 20-23--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---