Donnerstag, 11. Dezember 2008

Restful In Place Editor with CSRF protection and RJS response

It took me quite a while to get the in_place_editing in Ruby on Rails (Rails 2.1.2) to work with RESTful resources and using RJS to handle the data. So I thought I'd share my results and maybe save you some time.

The essential steps to the solutions are two patches:
* http://dev.rubyonrails.org/ticket/10055
* http://www.vaporbase.com/postings/Restful_In_Place_Editor

The first patch mainly takes care of sending the authenticity token with the in_place_editor.

The second patch applies RESTfulness but doesn't play well with the first patch, so a little more tweaking is necessary in in_place_macros_helper.rb by changing the bits displayed in bold face:
def in_place_editor
[...]
if protect_against_forgery? 
  options[:with] ||= "Form.serialize(form)" 
  options[:with] += " + '&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')" 
end  
+  if options[:as]
+    options[:with] ||= "Form.serialize(form)"
+    options[:with] += " + '&#{options[:as]}=' + encodeURIComponent(value)"
+  end
[...]
-  js_options['callback']   = "function(form,value) { return '#{options[:as]}=' + escape(value) }" if options[:as]
js_options['callback']   = "function(form,value) { return #{options[:with]} }" if options[:with]

Handling the data with RJS still raises the problem that after updating the value, the javascript of the RJS is displayed instead of the updated value. According to the documentation the solution to this is sending the option :script => :true.

The scriptaculous library (controls.js) switches the request method to get in that case, again breaking the RESTful approach. For this I found no other solution than to alter this behaviour in controls.js.

Old:
[...]
handleFormSubmission: function(e) {
[...]
  var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
New:
[...]
handleFormSubmission: function(e) {
[...]
+  if (this.options.restfulResponse)
+    var options = Object.extend({ evalScripts: false }, this.options.ajaxOptions);
+  else
     var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
For this change to be actually triggered, we need one more line in in_place_macros_helper.rb :
def in_place_editor
[...]
js_options['htmlResponse'] = !options[:script] if options[:script]
+   js_options['restfulResponse'] = true
Now, changing a framework library function is tricky as you cannot be 100% sure what the side effects might be and you have to reapply your changes with every update of the framework. So if any of you have a better solution, please tell me in the comments.

Donnerstag, 27. September 2007

Im August und September auf dem Nachttisch

  • Lawrence Norfolk, Lemprieres Wörterbuch
  • Terry Pratchett, Making Money
  • Erich Kästner, Fabian

Sonntag, 29. Juli 2007

Im Juli auf dem Nachttisch

  • Martin Suter, Die dunke Seite des Mondes
  • Pascal Mercier, Nachtzug nach Lissabon
  • T.C. Boyle, Drop City

Samstag, 30. Juni 2007

Im Juni auf dem Nachttisch

  • George R. R. Martin, A Song of Ice and Fire: A Storm of Swords
  • George R. R. Martin, A Song of Ice and Fire: A Feast of Crows

Montag, 4. Juni 2007

Im Mai auf dem Nachttisch

  • George R. R. Martin, A Song of Ice and Fire: A Game of Thrones
  • George R. R. Martin, A Song of Ice and Fire: A Clash of Kings

Montag, 21. Mai 2007

Nicht diskutieren, einfach machen

Es wird später niemand behaupten können, man konnte es nicht kommen sehen ...



via Spreeblick

Dienstag, 1. Mai 2007

Im April auf dem Nachttisch

  • Christopher Brookmyre, A Tale etched in Blood and hard black Pencil
  • Tad Williams, Shadowplay

Freitag, 20. April 2007

Der Wahnsinn gallopiert weiter

Wer bisher noch keinen Zweifel an Schäubles geistiger Gesundheit oder wahlweise seiner unsere Grundrechte achtenden Gesinnung hegte, möge sich hier eines Besseren belehren lassen:

Das law blog macht uns mit folgendem Zitat auf einen Stern Artikel aufmerksam:

Die Unschuldsvermutung heißt im Kern, dass wir lieber zehn Schuldige nicht bestrafen als einen Unschuldigen zu bestrafen. Der Grundsatz kann nicht für die Gefahrenabwehr gelten. Wäre es richtig zu sagen: Lieber lasse ich zehn Anschläge passieren, als dass ich jemanden, der vielleicht keinen Anschlag begehen will, daran zu hindern versuche. Nach meiner Auffassung wäre das falsch.

Oettinger wurde wegen seiner Filbinger-Entgleisung zu Recht sofort die Hölle heiß gemacht. Wo bleiben die Rücktrittsforderungen an unseren Innenminister, sollte er sich nicht augenblicklich von dieser Äußerung distanzieren?

Immerhin wird das Thema inzwischen auch in den etablierten Medien kritisch gewürdigt: Weitere Links zum Thema:

Donnerstag, 12. April 2007

Kurt Vonnegut

Kurt Vonnegut is dead. So it goes*.

I miss him already.

*: "When a Tralfamadorian sees a corpse, all he thinks is that the dead person is in a bad condition in that particular moment, but that same person is just fine in plenty of other moments. Now, when I myself hear that somebody is dead, I simply shrug and say what the Tralfamadorians say about dead people, which is 'So it goes'."

Montag, 26. März 2007

Im Februar und März auf dem Nachttisch

  • Andrew Sinclair, Gog
  • Frank McCourt, Teacher Man
  • Nick Hornby, The long way down
  • Stephen Fry, Moab is my Washpot
  • Paul Auster, Im Land der letzten Dinge
  • Stephen Fry, The Liar

Current rambling

Mir ist kotzschlecht.
Mir ist kotzschlecht.
fishy_ - 22. Mai, 09:38
Ahfo.
Ahfo.
fishy_ - 3. Mai, 11:56
FUP ist mir irgendwie...
FUP ist mir irgendwie durchgegangen. Das habe ich aber...
McLibboc - 2. Mai, 20:55
Und was ist mit FUP?
Und was ist mit FUP?
fishy_ - 2. Mai, 12:54
Vielleicht
war ich mit meiner Einschätzung der Medien doch...
McLibboc - 28. April, 10:45
Es ist wirklich unglaublich,...
Es ist wirklich unglaublich, dass bis jetzt kein Politiker,...
Bernhard (anonym) - 20. April, 12:42
Sehr schöner, klarsichtiger...
Sehr schöner, klarsichtiger Artikel!
Jörg (anonym) - 12. Februar, 19:52

Recent rambling

Juli 2009
Mo
Di
Mi
Do
Fr
Sa
So
 
 
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
 
 
 
 

Spot the loony

 

Ajax
Buecher
Korinthe
Muse
Musik
Zitate
Profil
Abmelden
Weblog abonnieren