Search code examples
ruby-on-railsautocompleteauthenticity-token

rails auto_complete plugin. how do i pass authenticity token?


I tried the auto_complete text field in rails 2.3.3 and the server says it denied request because of no authenticity token. I can see that the helper doesn't automatically create a parameter for it.

How I can manually do this? And I do not want to disable forgery prevention for this autocomplete.


Solution

  • Honestly, disabling the forgery protection isn't a bad idea if you scope this to just JS.

    def index
       respond_to |format| do
         format.html
         format.js do
           # your autocomplete code
         end
       end
    end
    

    Make your autocomplete call /things.js instead of /things.

    As far as I understand it, forgery protection is not needed for JS responses, and ensuring your autocomplete uses a GET method should also solve your problem. You're displaying a list, you're not modifying state, so use a GET and use the js response.