In-place editing in Rails
railsJust now, I wanted to add in-place editing to a Rails application. At first, I used jQuery’s Ajax.InPlaceEditor, and that worked (with a bit of authentication_token hackery), but I figured someone had done this before and had a Rails helper to do it.
Well, I found editable_content_tag, which mostly did the trick except that it wasn’t updating the page correctly. It turns out the controller was returning a nested JSON hash that looked something like this:
{'obj' => {'property_one': 'value', 'property_two', 'value two'}}
Their implementation of editable_content_tag wasn’t digging into the nested hash (i.e, it was using json.property_one instead of json.obj.property_one), and so it would display “undefined” when after being edited. Not cool.
Their implementation also didn’t work when a value was blank. It would generate a zero-width div, which wasn’t clickable. So I made it display “(not set)” when the value is blank.
Just in case anyone has encountered similar problems, here’s the modified code. To use it, put this into your app/helpers/application_helper.rb file.