In_place_editor_field for select tag

I need to have a in_place_edit_field for select tag.
I’ve been looking in /public/javascripts/console.js source and found
Ajax.InPlaceCollectionEditor JS class which seems to be the right one I
need.
But the problem is that I use ajax in rails first time and I don’t know
how to use it in my rails application. I can’t find anything in rails
API, either. I’ve only found in_place_editor_field which I
successfully use.

Thanks in advance for any suggestions.

RaW

Hello, I have the same problem.
Did you find a solution ???

cheers

Thanks Rob for your post but I’m lost within your code

On Oct 18, 2007, at 4:32 PM, Sig Dx wrote:

Thanks Rob for your post but I’m lost within your code

In my application, products are assigned to a category (Product
belongs_to Category)

For example:

In your view:
<%= in_place_collection_field
(product, :product, :category, @category_choices) %>

In the controller:

require ‘set_product_category’

class ProductsController < AuthorizedController
in_place_edit_for :product, :name

XHR /products/set_product_category/1

include SetProductCategory
end

In the set_product_category.rb file (Yes, I really have this comment…)

similar to having: in_place_edit_for :product, :category

except that it … actually, this probably could be abandoned!

module SetProductCategory
def set_product_category
@product = Product.find(params[:id], :include => :category)
if category = Category.find(params[:value])
@product.category = category
@product.save!
render :text => @product.category_name, :layout => false
else
render :text => “No category with ID #{params
[:value]}”, :status => 422
end
end
end

Rob B. http://agileconsultingllc.com
[email protected]

Hello Rob, thanks now it is much more clear, just a question:
why do you have set_product_category.rb file ??? isn’t it enough having
the set_product_category method within ProductsController ???

I’m a Rails newbie so maybe this question looks like silly :slight_smile:

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 =

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

cancel

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]

got it

On Oct 18, 2007, at 5:11 PM, Sig Dx wrote:

Hello Rob, thanks now it is much more clear, just a question:
why do you have set_product_category.rb file ??? isn’t it enough
having
the set_product_category method within ProductsController ???

I’m a Rails newbie so maybe this question looks like silly :slight_smile:

Posted via http://www.ruby-forum.com/.

I used that code in two places and putting in a separate module made
it DRY. :wink:

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs