Report abuse


			
# Controller:
  def show
    respond_to do |format|
      format.html
      format.json { render :json => @photo.to_json(:only => [:id, :caption, :title]) }
    end
  end

  def update
    @photo.attributes = params[:photo]
    if @photo.save
      flash[:notice] = "Photo saved."
      respond_to do |format|
        format.html { redirect_to [@photo.member, @photo] and return }
        format.js
      end
    else
      flash[:error] = "Could not save photo."
      respond_to do |format|
        format.html { render :action => "edit" }
        format.js
      end
    end
  end

# Helper:
def in_place_editor(object, field, options = {})
  options[:url] ||= object
  function =  "new Ajax.InPlaceEditor("
  function << "'#{object.class.to_s.downcase}_#{object.id}_#{field}', "
  function << "'#{url_for(options[:url])}'"
  js_options = {}
  if protect_against_forgery?
    options[:with] ||= "Form.serialize(form)"
    options[:with] += " + '&authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')"
  end
  options[:options] ||= '{method: "put"}'
  js_options["paramName"] = "'#{object.class.to_s.downcase}[#{field}]'"
  js_options['cancelText'] = %('#{options[:cancel_text]}') if options[:cancel_text]
  js_options['okText'] = %('#{options[:save_text]}') if options[:save_text]
  js_options['loadingText'] = %('#{options[:loading_text]}') if options[:loading_text]
  js_options['savingText'] = %('#{options[:saving_text]}') if options[:saving_text]
  js_options['rows'] = options[:rows] if options[:rows]
  js_options['cols'] = options[:cols] if options[:cols]
  js_options['size'] = options[:size] if options[:size]
  js_options['externalControl'] = "'#{options[:external_control]}'" if options[:external_control]
  js_options['loadTextURL'] = "'#{url_for(options[:load_text_url])}'" if options[:load_text_url]        
  js_options['ajaxOptions'] = options[:options] if options[:options]
  js_options['htmlResponse'] = !options[:script] if options[:script]
  js_options['callback']   = "function(form) { return #{options[:with]} }" if options[:with]
  js_options['clickToEditText'] = %('#{options[:click_to_edit_text]}') if options[:click_to_edit_text]
  js_options['textBetweenControls'] = %('#{options[:text_between_controls]}') if options[:text_between_controls]
  js_options['onComplete'] = "function(transport, element) { eval(transport.responseText); }"
  js_options['onFailure'] = "function(transport) { eval(transport.responseText); }"
  function << (', ' + options_for_javascript(js_options)) unless js_options.empty?   
  function << ").loadExternalText = function() {
    this._form.addClassName(this.options.loadingClassName);
    this._controls.editor.disabled = true;
    var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
    options.method = \"get\";
    Object.extend(options, {
      parameters: 'editorId=' + encodeURIComponent(this.element.id),
      onComplete: Prototype.emptyFunction,
      onSuccess: function(transport) {
        this._form.removeClassName(this.options.loadingClassName);
        var text = transport.responseText.evalJSON()['#{object.class.to_s.downcase}']['#{field}'];
        this._controls.editor.value = text;
        this._controls.editor.disabled = false;
        this.postProcessEditField();
      }.bind(this),
      onFailure: this._boundFailureHandler
    });
    new Ajax.Request(this.options.loadTextURL, options);
  };"
  javascript_tag(function)
end  

# View:
<%= in_place_editor(@photo, :caption, :url => [@photo.member, @photo],
  :load_text_url => formatted_member_photo_path(@photo.member, @photo, :json)) %>

# Fugly, embarassing code by Norman Clarke (http://randomba.org)