Forum: Ruby on Rails In_place_editor in a For loop

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David S. (Guest)
on 2006-03-01 14:15
(Received via mailing list)
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 %>
         <li id="feed-<%= feed.id %>"><%= in_place_editor_field
(:feed, :url) %> - <%= link_to_remote "Delete", :update =>
"feed", :url => { :action => "destroy_feed", :id => feed.id } %></li>
      <% 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
unknown (Guest)
on 2006-03-01 23:57
(Received via mailing list)
On 3/1/06, David S. <removed_email_address@domain.invalid> 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:
    ...
      <tbody id="xsbody">
        <% for x in @xs %>
          <tr id="x_<%= x.id %>">
            <%= render :partial => 'x', :object => x %>
          </tr>
        <% end %>
      </tbody>
    ...

  _x.rhtml:
    ...
        <% for field in @fields %>
          <td>
            <%= editable_content_for host, field %>
          </td>
        <% 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
Mark Reginald J. (Guest)
on 2006-03-02 00:31
(Received via mailing list)
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.
This topic is locked and can not be replied to.