in_place_editor_field and multiple element updates

Posted by Paul Haddad Fri, 30 Nov 2007 01:50:00 GMT

This one took me a while to figure out so hopefully it'll be useful for someone out there. I had a fairly simple need, an in_place_editor_field that updates multiple elements. The main trick here is to make sure the :script => true option gets passed in.

In the View do the following
<%= in_place_editor_field :asset, :name,
 { :id => "in_place_editor_#{@asset.dom_id}" }, 
:script => true %>
And in the Controller
  def set_asset_name
    asset  = Asset.find_by_id(params[:id])
    old_value = asset.name
    asset.name = params[:value]
    asset.name = old_value unless asset.save

    render :update do | page |
      page.replace_html "edit_#{asset.dom_id}", asset.name  
      page.replace_html "in_place_editor_#{asset.dom_id}", asset.name  
    end
  end
Again the key is to make sure the :script => true option is set, otherwise you'll get the JS code generated by the render :update instead of the new value.

Posted in  | Tags , ,  | no comments