Improved in_place_select_editor
Posted by Bhushan Ahire | Posted in Rails | Posted on 03-09-2007
0
The in_place_select_editor method assumes that you want to display the exact item being edited. That isn’t always the case. You might want to display a related attribute. The following modifies in_place_select_editor_field by allowing it to take a display parameter (third parameter), which is then passed to to_content_tag_display (also modified)
module ApplicationHelper def in_place_select_editor(field_id, options = {}) function = "new Ajax.InPlaceSelectEditor(" function << "'#{field_id}', " function << "'#{url_for(options[:url])}'" function << (', ' + options_for_javascript( { 'selectOptionsHTML' => %('#{escape_javascript(options[:select_options].gsub(/n/, ""))}') } ) ) if options[:select_options] function << ')' javascript_tag(function) end def in_place_select_editor_field(object, method, display, tag_options = {}, in_place_editor_options = {}) tag = ::ActionView::Helpers::InstanceTag.new(object, method, self) tag_options = { :tag => "span", :id => "#{object}_#{method}_#{tag.object.id}_in_place_editor", :class => "in_place_editor_field"}.merge!(tag_options) in_place_editor_options[:url] = in_place_editor_options[:url] || url_for({ :action => "set_#{object}_#{method}", :id => tag.object.id }) tag.to_content_tag_display(tag_options.delete(:tag), display, tag_options) + in_place_select_editor(tag_options[:id], in_place_editor_options) end end module ActionView module Helpers class InstanceTag #:nodoc: include Helpers::TagHelper def to_content_tag_display(tag_name, display, options = {}) content_tag(tag_name, display, options) end end end end
This permits (example), where Job.vendor_name(@job) (a custom method) is displayed.
<%= in_place_select_editor_field( :job, :vendor_id, Job.vendor_name(@job), {}, :select_options => options_from_collection_for_select(@vendors, 'id', 'name', @job.vendor_id)) %>
you could also use this to display an icon to click on to display the drop-down box by passing an image_tag as the third parameter.
