On 3/1/06, David Smalley <david.smalley.lists@googlemail.com>
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