In_place_editor in a For loop

I’m using Ajax to create a list of items. Once these items have been
created the user can edit in place and delete them. I’ve got the ajax
working for delete and removing the element from the page, but the
in_place_editor_field doesn’t seem to like being in a for loop. I’ve
got in_place_editor working for single items, but what’s the syntax
for creating editors for a list of items.

Here’s my code so far:

<% if @feeds -%>
<% for feed in @feeds %>

  • <%= in_place_editor_field
    (:feed, :url) %> - <%= link_to_remote “Delete”, :update =>
    “feed”, :url => { :action => “destroy_feed”, :id => feed.id } %>

  • <% end -%>
    <% end -%>

    The error I am getting back is:

    Called id for nil, which would mistakenly be 4 – if you really
    wanted the id of nil, use object_id

    I’m assuming this is because the in_place_editor is being called with
    an incorrect id, the backtrace points to this line of the javascript
    macro helper file:

    tag_options = {:tag => “span”, :id => “#{object}#{method}#
    {tag.object.id}_in_place_editor”, :class =>
    “in_place_editor_field”}.merge!(tag_options)

    Any ideas on syntax to solve this?

    David

    David S.
    w: http://davidsmalley.com/blog

    On 3/1/06, David S. [email protected] wrote:

    I’m using Ajax to create a list of items. Once these items have been
    created the user can edit in place and delete them. I’ve got the ajax
    working for delete and removing the element from the page, but the
    in_place_editor_field doesn’t seem to like being in a for loop. I’ve
    got in_place_editor working for single items, but what’s the syntax
    for creating editors for a list of items.

    Any ideas on syntax to solve this?

    Based on Coda Hale’s article:
    “A Rails HOWTO: Simplify In-Place Editing with Script.aculo.us
    http://blog.codahale.com/2006/01/14/
    a-rails-howto-simplify-in-place-editing-with-scriptaculous

    This is from a recent project I worked on:

    _xs.rhtml:


    <% for x in @xs %>

    <%= render :partial => ‘x’, :object => x %>

    <% end %>

    _x.rhtml:

    <% for field in @fields %>

    <%= editable_content_for host, field %>

    <% end %>

    application_helper.rb:
    def editable_content_for object, method, ajax = {}
    editable_content(
    :content => {
    :element => ‘span’,
    :text => object.method(method.to_s).call,
    :field => method.to_s,
    :options => {
    :id =>
    “#{object.class.to_s}_#{method.to_s}edit#{object.id}”,
    :class => ‘editable-content’
    }
    },
    :url => {
    :controller => “#{object.class.to_s}s”,
    :action => “indate”,
    :id => object.id
    },
    :ajax => ajax
    )
    end

      def editable_content(options)
        options[:content] = { :element => 'span' 
    

    }.merge(options[:content])
    options[:url] = {}.merge(options[:url])
    options[:ajax] = { :okText => “‘Save’”, :cancelText =>
    “‘Cancel’”}.merge(options[:ajax] || {})
    options[:ajax].merge!({ :callback => “function(form, value) {
    return ‘field=#{options[:content][:field]}&value=’ + escape(value) }”
    })
    script = Array.new
    script << “new Ajax.InPlaceEditor(”
    script << " ‘#{options[:content][:options][:id]}’,"
    script << " ‘#{url_for(options[:url])}’,"
    script << " {"
    script << options[:ajax].map{ |key, value| “#{key.to_s}:
    #{value}” }.join(", “)
    script << " }”
    script << “)”

        options[:content][:text] = "-click to enter-" if
    

    options[:content][:text].to_s.size < 1
    content_tag(
    options[:content][:element],
    options[:content][:text],
    options[:content][:options]
    ) + javascript_tag( script.join(“\n”) )
    end

    David S. wrote:

    the id of nil, use object_id
    Try using <% for @feed in @feeds %>


    We develop, watch us RoR, in numbers too big to ignore.