views:

460

answers:

3

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.

A: 

The forgery prevention is part of the form helper, not the field helper. If you use the full RoR form helper it should work. If it doesn't, please edit your question to include the form code and I'll try to help.

Mike Buckbee
+2  A: 

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.

Brian Hogan
A: 

having had a similar problem I found just adding ":method => :get" to the "text_field_with_auto_complete" tag fixed it (as per Brian) - I didn't need to disable forgery protection

Straff