On Oct 18, 2007, at 3:00 AM, Sig Dx wrote:
Hello, I have the same problem.
Did you find a solution ???
cheers
I don’t know when this thread started, but I’ve been using this for
months now (it lives in app/helpers/application_helper.rb):
Example:
class Soldier < ActiveRecord::Base; belongs_to :rank; end
class Rank < ActiveRecord::Base; has_many :soldiers;
attr_accessible :name; end
Arguments:
object:: The actual object containing the
field (e.g.,
Soldier.find(params[:id]))
obj_name:: The name to be used for constructing
HTML id’s
and such (e.g., ‘soldier’ which
becomes a
problem if abject is expected to be
instance_get("@#{obj_name}"), but it’s
actaually a local in a partial)
method:: The +String+ naming the method for
the association (e.g., ‘rank’)
choices:: +:all+ means get them from the named
association with the ‘text’ coming
from :name
and ‘value’ coming from :id,
otherwise, a
+container+ fitting the description from
+options_for_select+
def in_place_collection_field(object, obj_name, method, choices,
tag_options = {},
in_place_selector_options = {})
tag = ::ActionView::Helpers::InstanceTag.new(obj_name, method,
self, nil, object)
choices = object.send(method).class.find(:all).collect {|o|
[o.name, o.id] } if choices == :all
tag_options = { :tag => “span”, :class => “in_place_select_field”,
:id => “#{obj_name}#{method}#{object.id}
in_place_selector"}.merge!(tag_options)
in_place_selector_options[:url] ||= url_for({ :action => "set#
{obj_name}_#{method}”,
:id => object.id })
# in_place_selector_options = { :ok_button =>
false, :submit_on_blur => true }.merge!(in_place_selector_options)
name = object.send(method).name rescue ‘(none)’
content_tag(tag_options.delete(:tag), name, tag_options) +
in_place_collection_editor(tag_options[:id], choices,
in_place_selector_options)
end
#–
Based on
ActionView::Helpers::JavaScriptMacrosHelper::in_place_editor
from Rails 1.2.2 (ActionPack 1.13.2)
vendor/rails/actionpack/lib/action_view/helpers/
java_script_macros_helper.rb
#++
----
Makes an HTML element specified by the DOM ID +field_id+ become
an in-place
editor of a property using a <select%gt; element.
It is as if a form is automatically created and displayed when
the user
clicks the element, something like this:
chair
easel
stool
table
armoire
The form is serialized and sent to the server using an AJAX
call, the action on
the server should process the value and return the updated value
in the body of
the reponse. The element will automatically be updated with the
changed value
(as returned from the server).
Required +options+ are:
:url:: Specifies the url where the updated value
should
be sent after the user presses “ok”.
Addtional +options+ are:
:cancel_text:: The text on the cancel link.
(default: “cancel”)
:save_text:: The text on the save link.
(default: “ok”)
:loading_text:: The text to display while the data
is being loaded from the server (default: “Loading…”)
:saving_text:: The text to display when
submitting to the server (default: “Saving…”)
:external_control:: The id of an external control used
to enter edit mode.
:load_text_url:: URL where initial value of editor
(content) is retrieved.
:options:: Pass through options to the AJAX
call (see prototype’s Ajax.Updater)
:with:: JavaScript snippet that should
return what is to be sent
in the AJAX call, +form+ is an
implicit parameter
:script:: Instructs the in-place editor to
evaluate the remote JavaScript response (default: false)
:click_to_edit_text::The text shown during mouseover
the editable text (default: “Click to edit”)
def in_place_collection_editor(field_id, choices=[], options = {})
function = “new Ajax.InPlaceCollectionEditor(”
function << "’#{field_id}’, "
function << “’#{url_for(options[:url])}’”
js_options = {}
js_options['collection'] = choices.map {|choice| [ choice.last,
choice.first ] }.to_json
js_options['okButton'] = options[:ok_button] if options.has_key?
(:ok_button)
js_options[‘submitOnBlur’] = options[:submit_on_blur] if
options.has_key?(:submit_on_blur)
js_options[‘okText’] = %(’#{options[:save_text]}’) if options
[:save_text]
js_options[‘cancelText’] = %(’#{options[:cancel_text]}’) if
options[:cancel_text]
js_options[‘loadingText’] = %(’#{options[:loading_text]}’) if
options[:loading_text]
js_options[‘loadTextURL’] = “’#{url_for(options
[:load_text_url])}’” if options[:load_text_url]
js_options[‘savingText’] = %(’#{options[:saving_text]}’) if
options[:saving_text]
js_options[‘callback’] = “function(form) { return #{options
[:with]} }” if options[:with]
js_options['externalControl'] = "'#{options
[:external_control]}’" if options[:external_control]
js_options[‘ajaxOptions’] = options[:options] if options[:options]
js_options['evalScripts'] = options[:script] if options[:script]
js_options['paramName'] = %('#{options[:param_name]}') if options
[:param_name]
js_options[‘highlightcolor’] = %(’#{options[:highlightcolor]}’)
if options[:highlightcolor]
js_options[‘highlightendcolor’] = %(’#{options
[:highlightendcolor]}’) if options[:highlightendcolor]
js_options[‘onFailure’] = %(’#{options[:on_failure]}’) if options
[:on_failure]
js_options[‘savingClassName’] = %(’#{options
[:saving_class_name]}’) if options[:saving_class_name]
js_options[‘clickToEditText’] = %(’#{options
[:click_to_edit_text]}’) if options[:click_to_edit_text]
js_options[‘cancelLink’] = options[:cancel_link] if
options.has_key?(:cancel_link)
function << (', ' + options_for_javascript(js_options)) unless
js_options.empty?
function << ')'
javascript_tag(function)
end
Rob B. http://agileconsultingllc.com
[email protected]