I have looked around and I am either not understanding something, or just can''t find a way to get a javascript variable posted to into rails. I have a javascript that runds and takes input from a user, and I need to get these javascript values into the rails app on the server. Since this is within a script the ruby link_to_remote and alike functions won''t work since I need to be within the javascript script, so I need an example of a javascript/prototype ajax function or just a submit or post and how I can read variables passed between on the server side. Any help would be much obliged --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can make a direct ajax call var myvar = ''something''; var url = ''/some_controller/some_action? myvar=''+encodeURIComponent(myvar); new Ajax.Request(url, { asynchronous:true, evalScripts:true, method:''get'', onComplete: function(t) { // do something } }); On Mar 30, 4:19 pm, sweiss <weissste...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> I have looked around and I am either not understanding something, or > just can''t find a way to get a javascript variable posted to into > rails. I have a javascript that runds and takes input from a user, and > I need to get these javascript values into the rails app on the > server. Since this is within a script the ruby link_to_remote and > alike functions won''t work since I need to be within the javascript > script, so I need an example of a javascript/prototype ajax function > or just a submit or post and how I can read variables passed between > on the server side. > > Any help would be much obliged--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks, and I have seen some things kind of like this, but what I can''t find is the server side code, but how do I read that myvar in the controller --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That code snippet with send an http request to the specified url (which will be routed to a controller)... so var content = ''something''; var url = ''/posts/create? message=''+encodeURIComponent(content); new Ajax.Request(url, { asynchronous:true, evalScripts:true, method:''get'', onComplete: function(t) { // do something } }); Let''s say that following goes to posts/create and it has the variable attached to it so the full url is: /posts/create?message=something This will end an ajax request to the "create" action of the "posts" controller (assuming the routes) and the "message" param appended at the end will be accessible in the action via params[:message] so inside /app/controllers/posts_controller.rb def create Post.new(params[:message]) # params[:message] => ''something'' end -- 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 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 -~----------~----~----~----~------~----~------~--~---
You will probably save yourself some effort here if you create a js function that you include in the page (e.g., application.js or another js file you include) and let that function accept the url or an options hash through which you pass the url. That way you can rely on Rails'' helpers to build the url for you, decoupling your js from the Routing system currently used. As for the Ajax.Request, the posted methods will work but they are a bit messy. Prototype''s method actually has a ''parameters'' option that handles the variables in a cleaner fashion. For example: my_update = function(url) { ... new Ajax.Request(url, parameters: {var_1: encodeURICompoment(var1), var_2: encodeURIComponent(var2), ...}, asynchronous:true, evalScripts:true, method: ''put''} } On Mar 30, 7:36 pm, rubynuby <dear...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You can make a direct ajax call > > var myvar = ''something''; > var url = ''/some_controller/some_action? > myvar=''+encodeURIComponent(myvar); > new Ajax.Request(url, > { asynchronous:true, evalScripts:true, method:''get'', > onComplete: function(t) { // do something > } > > }); > > On Mar 30, 4:19 pm, sweiss <weissste...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > I have looked around and I am either not understanding something, or > > just can''t find a way to get a javascript variable posted to into > > rails. I have a javascript that runds and takes input from a user, and > > I need to get these javascript values into the rails app on the > > server. Since this is within a script the ruby link_to_remote and > > alike functions won''t work since I need to be within the javascript > > script, so I need an example of a javascript/prototype ajax function > > or just a submit or post and how I can read variables passed between > > on the server side. > > > Any help would be much obliged--~--~---------~--~----~------------~-------~--~----~ 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 31 Mar 2008, at 13:19, AndyV wrote:> > You will probably save yourself some effort here if you create a js > function that you include in the page (e.g., application.js or another > js file you include) and let that function accept the url or an > options hash through which you pass the url. That way you can rely on > Rails'' helpers to build the url for you, decoupling your js from the > Routing system currently used.Also don''t forget about the :with option (eg link_to_remote ''Click me'', :url => {...}, :with => ''put some javascript here'') Fred> > > As for the Ajax.Request, the posted methods will work but they are a > bit messy. Prototype''s method actually has a ''parameters'' option that > handles the variables in a cleaner fashion. For example: > > my_update = function(url) { > ... > new Ajax.Request(url, parameters: {var_1: encodeURICompoment(var1), > var_2: encodeURIComponent(var2), ...}, asynchronous:true, > evalScripts:true, method: ''put''} > } > > On Mar 30, 7:36 pm, rubynuby <dear...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> You can make a direct ajax call >> >> var myvar = ''something''; >> var url = ''/some_controller/some_action? >> myvar=''+encodeURIComponent(myvar); >> new Ajax.Request(url, >> { asynchronous:true, evalScripts:true, method:''get'', >> onComplete: function(t) { // do something >> } >> >> }); >> >> On Mar 30, 4:19 pm, sweiss <weissste...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: >> >>> I have looked around and I am either not understanding something, or >>> just can''t find a way to get a javascript variable posted to into >>> rails. I have a javascript that runds and takes input from a user, >>> and >>> I need to get these javascript values into the rails app on the >>> server. Since this is within a script the ruby link_to_remote and >>> alike functions won''t work since I need to be within the javascript >>> script, so I need an example of a javascript/prototype ajax function >>> or just a submit or post and how I can read variables passed between >>> on the server side. >> >>> Any help would be much obliged > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---