in_place_select with hash?

Hi Folks,

I have recently starting working with Flvorful's 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: </b><%= 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: </b><%= 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. :slight_smile:

I'm sure I have simply overlooked something.

Many thanks,

pdp

I found the solution (well, at least "a" solution). This post,
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

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

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.