Forum: Ruby on Rails Calling an action within javascript function w/ ajax

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
David (Guest)
on 2008-12-14 06:02
(Received via mailing list)
I am trying to call an action within a javscript function using ajax,
but I am having some problems.  I need the ajax call to return a value
and am thinking I can do this using onComplete and Ajax.Request but I
am running into some problems.  This is the code I am using for the

    new Ajax.Request( '/users/username', {
      method: 'post',
      parameters: { username: $('new_user_username').value},
      onComplete: parseResults()

I have an action called username in my users controller in which I am
taking the username params passed by the Ajax.Request and seeing if it
exists in the database.  I want to return true/false depending on if
it exists or not.  But I am having trouble even getting the
Ajax.Request to work.

This is some of the output from the console:

Processing UsersController#username (for at 2008-12-13
19:52:39) [POST]
  Session ID:
  Parameters: {"username"=>"d", "action"=>"username",


Im not sure how to get around this InvalidAuthenticityToken, anyone
have any idea whats going on here.  Ive been struggling with this for
a while and am somewhat new to ajax so I could use some help.
David (Guest)
on 2008-12-14 06:04
(Received via mailing list)
Im thinking this needs to be a 'GET' request in order to return a
response, but when I change the method to a GET, it calls the show

  Parameters: {"username"=>"f", "action"=>"show", "id"=>"username",

hyuan (Guest)
on 2008-12-16 05:04
(Received via mailing list)
Just use remote_function and it will generate the appropriate ajax
call for you - this will make your code much less brittle to Rails

This topic is locked and can not be replied to.