svētdiena, 2008. gada 17. februāris

Problem with auto_complete_for and protect_from_forgery

I was having problem with rails plugin auto_complete.
The problem is that when you use protect_from_forgery then auto_complete gives error.

Rails version: Rails 2.0.2

This I have in controller:
auto_complete_for :userEquipment, :name

This I have in view:
text_field_with_auto_complete :userEquipment, :name

But I was having such error:
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

The answer is here in file auto_complete_macros_helper.rb

You need to change this line:
auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
To this line:
auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}", :authenticity_token => "#{form_authenticity_token}" } }.update(completion_options))

And now it's working!

But I'm new in Rails, so maybe there is nicer way. :)

Nav komentāru: