# In\_place\_editor\_field for select tag

**URL:** <https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648 "2007-01-19T16:32:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [January 19, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/1 "2007-01-19T16:32:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [October 18, 2007, 9:00am UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/2 "2007-10-18T09:00:02Z")

</div>

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

cheers

---

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [October 18, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/3 "2007-10-18T22:32:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [October 18, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/4 "2007-10-18T22:54:36Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [October 18, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/5 "2007-10-18T23:11:35Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [October 18, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/6 "2007-10-18T13:11:06Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [October 19, 2007, 1:58am UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/7 "2007-10-19T01:58:55Z")

</div>

got it

---

<div class="post-metadata">

**Author:** ![RaW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@RaW](https://www.ruby-forum.com/u/RaW)\
**Post date:** [October 19, 2007, 12:00am UTC](https://www.ruby-forum.com/t/in-place-editor-field-for-select-tag/82648/8 "2007-10-19T00:00:41Z")

</div>

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 🙂
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

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

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
