# InPlaceSelectEditor question

**URL:** <https://www.ruby-forum.com/t/inplaceselecteditor-question/99675>\
**Category:** Rails\
**Created:** [July 24, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/inplaceselecteditor-question/99675 "2006-07-24T21:43:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [July 24, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/inplaceselecteditor-question/99675/1 "2006-07-24T21:43:52Z")

</div>

I’m attempting to use the example given in the Rails Recipes book  
chapter 2,  
to create a select drop-down using the InPlaceEditor function (modified  
to  
be InPlaceSelectEditor.

The example given works (a country list drop-down), but I’m attempting  
to  
use it to display a list of items generated from an object in another  
table  
that is linked to the table that I’m editing. That also works except  
that it  
displays the ‘id’ instead of the related value of the id in the other  
table.  
Sorry if that isn’t clear. Here’s the relevant code.

Table item contains language\_id (among other fields, of course)  
Table language contains id, lang (the latter having the language in  
English)

```
   <% for @item in job.items %>
      <tr>
        <td><%= in_place_editor_field :item, :title %></td>
        <td><%= in_place_select_editor_field(
         :item,
         :language_id,
         {},
         :select_options =>

```

options\_from\_collection\_for\_select(@languages, ‘id’, ‘lang’)) %\>  
  
( … more code … )  
  
\<% end %\>

It works okay except that it displays item.language\_id (which when  
clicked,  
correctly shows the list of languages, not their ids), when what I want  
it  
to display is item.language.lang (not the id of the language). However,  
the  
in\_place\_select\_editor helper doesn’t seem to have on option to override  
the  
value that is displayed. It just automatically displays the  
object/method of  
the first two options. I presume I could extend the helper to take an  
additional option that would be displayed, but I don’t know any JS so  
I’m  
not sure how to do that. (Sorry, I’m a beginner.)

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [January 5, 2007, 9:58am UTC](https://www.ruby-forum.com/t/inplaceselecteditor-question/99675/2 "2007-01-05T09:58:35Z")

</div>

Salut FranÃ§ois,

thanks for the code snipplet - it works indeed, but only with one little  
drawback: as soon as the value is submitted, the text value is shown as  
the id again. If one clicks the field again to edit it, the textual  
value is shown properly again.

Is there a way to display the desired text value instead of the  
numerical id -after- the editing is done (= the new value has been  
submitted)? Or is this the way how it should work after all and there’s  
only something wrong on my end?

Cheers,  
carp

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [July 25, 2006, 6:09am UTC](https://www.ruby-forum.com/t/inplaceselecteditor-question/99675/3 "2006-07-25T06:09:29Z")

</div>

Finally solved this problem after digging deep into Rails’ innards. Am  
noting solution here for those who may run into the same thing. There  
are probably better ways, but this worked for me.

the in\_place\_select\_editor\_field calls the IntanceTag.to\_content\_tag  
method, which is as follows:

```
  def to_content_tag(tag_name, options = {})
    content_tag(tag_name, value, options)
  end

  def value
    unless object.nil?
      object.send(@method_name)
    end
  end

```

So as you can see, it just assumes that the value to be displayed in the  
tag is the value of the method passed to the tag.

So I extended the InstanceTag class by adding a new  
‘to\_content\_tag\_display’ method. I added the following to my  
application\_helper.rb (maybe there’s a better place to put this kind of  
stuff? I don’t really know.)

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

Then I modified my in\_place\_select\_editor\_field to accept an additional  
display parameter and to call to\_content\_tag\_display passing to it the  
same additional parameter:

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

Then in my view, I added an additional parameter:

\<%= in\_place\_select\_editor\_field(  
:item,  
:language\_id,  
@item.language.lang,  
{},  
:select\_options =\>  
options\_from\_collection\_for\_select(@languages, ‘id’, ‘lang’)) %\>

the third parameter (in this case @item.language.lang) is the value to  
be displayed.

PS. This might make a useful patch?

FranÃ§ois Montel wrote:

> I’m attempting to use the example given in the Rails Recipes book  
> chapter 2,  
> to create a select drop-down using the InPlaceEditor function (modified  
> to  
> be InPlaceSelectEditor.
> 
> The example given works (a country list drop-down), but I’m attempting  
> to  
> use it to display a list of items generated from an object in another  
> table  
> that is linked to the table that I’m editing. That also works except  
> that it  
> displays the ‘id’ instead of the related value of the id in the other  
> table.  
> Sorry if that isn’t clear. Here’s the relevant code.
> 
> Table item contains language\_id (among other fields, of course)  
> Table language contains id, lang (the latter having the language in  
> English)
> 
> ```
> <% for @item in job.items %>
> <tr>
> <td><%= in_place_editor_field :item, :title %></td>
> <td><%= in_place_select_editor_field(
> :item,
> :language_id,
> {},
> :select_options =>
> 
> ```
> 
> options\_from\_collection\_for\_select(@languages, ‘id’, ‘lang’)) %\>  
>   
> ( … more code … )  
>   
> \<% end %\>
> 
> It works okay except that it displays item.language\_id (which when  
> clicked,  
> correctly shows the list of languages, not their ids), when what I want  
> it  
> to display is item.language.lang (not the id of the language). However,  
> the  
> in\_place\_select\_editor helper doesn’t seem to have on option to override  
> the  
> value that is displayed. It just automatically displays the  
> object/method of  
> the first two options. I presume I could extend the helper to take an  
> additional option that would be displayed, but I don’t know any JS so  
> I’m  
> not sure how to do that. (Sorry, I’m a beginner.)
> 
> Thanks for the help!
