# In\_place\_select with hash?

**URL:** <https://www.ruby-forum.com/t/in-place-select-with-hash/133219>\
**Category:** Rails\
**Created:** [March 28, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/in-place-select-with-hash/133219 "2008-03-28T22:46:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [March 28, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/in-place-select-with-hash/133219/1 "2008-03-28T22:46:39Z")

</div>

Hi Folks,

I have recently starting working with Flvorful’s Super In Place Controls  
([http://os.flvorful.com/super\_in\_place\_controls](http://os.flvorful.com/super_in_place_controls)) and have a question  
with which I hope someone can help.

I would like to use either the in\_place\_select or in\_place\_radio and  
have it display one value, but record another. Without the in place  
editing, I might have have used a radio button to display “Active” while  
recording “1” in my database (using an integer to record the state  
instead of a string, obviously).

The rough code is this (this works correctly in that is displays and  
records the status integer correctly):

Status: \<%= in\_place\_select :contestGroup, :status,  
:choices =\> %w(0 1).map {|e| [e,e]} %\>

I have been able to use the following some what successfully:

Status: \<%= in\_place\_select :contestGroup, :status,  
:choices =\> {“Disabled” =\> “0”, “Active” =\> “1”}.each {|k,v| [k,v]} %\>

This will correctly update the database with the new status, but the  
display in the select box is either “0” or “1”. If I reverse the hash,  
naturally, I see “Disabled” or “Active” in the select box, but then the  
string – not the number – is passed back in the AJAX handler.

Finally, I only ever see a “0” or “1” before activating the  
in\_place\_select.

I would greatly appreciate if anyone with some experience with this  
plugin and has been able to control the display and update accurately  
could share some wisdom. 🙂

I’m sure I have simply overlooked something.

Many thanks,

pdp

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [March 31, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/in-place-select-with-hash/133219/2 "2008-03-31T20:09:10Z")

</div>

I found the solution (well, at least “a” solution). This post,  
[Can't get select helper to populate dropdown w/default value - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/50312#17342), identified that the  
“collect” method as the secret sauce.

Here’s an example:

irb(main):037:0 testHash = {“public” =\> 0, “private” =\> 1}  
=\> {“public”=\>0, “private”=\>1}  
irb(main):038:0\> eachResults = testHash.each { |k,v| [k,v] }  
=\> {“public”=\>0, “private”=\>1}  
irb(main):039:0\> collectResults = testHash.collect { |k,v| [k,v] }  
=\> [[“public”, 0], [“private”, 1]]

The nested array is what we need in the select helper. The each method  
returns the object instance while the collect method returns a new  
array.

I hope this helps someone in the future.

- pdp

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [March 31, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/in-place-select-with-hash/133219/3 "2008-03-31T20:14:14Z")

</div>

Peter Degen-Portnoy wrote:

> I found the solution (well, at least “a” solution).

Yes, but not to the original question. I have the select box working  
correctly (displaying text and setting a number), but the display field  
(when the in\_place\_select is not active) is showing the value and not  
the display text.

- pdp

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [August 13, 2008, 11:40am UTC](https://www.ruby-forum.com/t/in-place-select-with-hash/133219/4 "2008-08-13T11:40:03Z")

</div>

Peter Degen-Portnoy wrote:

> Peter Degen-Portnoy wrote:
> 
> > I found the solution (well, at least “a” solution).
> 
> Yes, but not to the original question. I have the select box working  
> correctly (displaying text and setting a number), but the display field  
> (when the in\_place\_select is not active) is showing the value and not  
> the display text.
> 
> - pdp

Hi pdp,

did you manage to solve this? I have the same problem. Bugs the sh… out  
of me too.  
Hope you or anyone else have a solution.  
Thanks

Mike

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [June 20, 2009, 12:27am UTC](https://www.ruby-forum.com/t/in-place-select-with-hash/133219/5 "2009-06-20T00:27:33Z")

</div>

Michael Yard wrote:

> Peter Degen-Portnoy wrote:
> 
> > Peter Degen-Portnoy wrote:
> > 
> > > I found the solution (well, at least “a” solution).
> > 
> > Yes, but not to the original question. I have the select box working  
> > correctly (displaying text and setting a number), but the display field  
> > (when the in\_place\_select is not active) is showing the value and not  
> > the display text.
> > 
> > - pdp
> 
> Hi pdp,
> 
> did you manage to solve this? I have the same problem. Bugs the sh… out  
> of me too.  
> Hope you or anyone else have a solution.  
> Thanks
> 
> Mike

Hi Mike,

I picked up this code again after it sat unattended for a while.

I found that using the :display\_text option solved the problem of  
showing an intelligent value in the selection list while retaining the  
ability to record the more efficient numerical representation to the  
database.

Here’s how it looks now:

in\_place\_select :contestSeries, :status,  
:choices =\> {“Disabled” =\> “0”, "Active =\> “1”}.collect {|k,v| [k,v]},  
:display\_text =\>  
convert\_contest\_series\_status\_codes(@contestSeries.status)

“convert\_contest\_series\_status\_codes” is a helper method used in a  
variety of locations to convert “0” to “Disabled” and “1” to “Active”.  
Basically having something get the display string and pass it to  
:display\_text shows the correct value in the selection list.

Unfortunately, the display\_text does not update after the value is  
changed. One has to refresh the page after updating the value, so there  
is still some more work to do.
