Setting empty fields with in_place_editor_field

Hi all!

Playing around with in_place_editor_field, I found that when you set a
field to be empty, you can’t edit it at all anymore after that. Am I
doing something wrong or is this the way it is intended to be? If so, is
there any way around that behaviour? Is it just styling?

Best regards,
Raphael

Does really nobody use in_place_editor_field like that, for setting a
previously empty field to some value?

On 3/24/06, Raphael S. [email protected] wrote:

Does really nobody use in_place_editor_field like that, for setting a
previously empty field to some value?

Whereever I use an in_place_editor, I make sure that nil values cannot
be saved in the controller. Instead of nil, I save “Unknown” or
something similar.

-eric

That’s a simple yet wonderful idea – I’ll go with a dash. Thanks a lot
Eric!

Raphael S. wrote:

Does really nobody use in_place_editor_field like that, for setting a
previously empty field to some value?

Here’s a way I’ve been experimenting with:

<% for column in Books.content_columns %>

<%= column.human_name %>: <%= in_place_editor_field :books, column.name, {}, {:external_control => column.name } -%>

<% end %>

That makes both the value and label clickable.

The only issue is that editing causes the label to disappear. Can anyone
say if this is the proper behavior? The docs say “:external_control::
The id of an external control used to enter edit mode.” and to me that
doesn’t give an indication that it should disappear.

Regardless, it does allow for editing empty fields.

Curtis

Curtis Hatter wrote:

":external_control:: The id of an external control used to enter edit

Here’s a way to fix the in_place_editor so that it doesn’t hide the
label.

in ‘public/javascripts/controls.js’ comment out lines 513-515 (this
going off most recent edge rails). The lines are:

if (this.options.externalControl) {
Element.hide(this.options.externalControl);
}

Hope that gives the effect you are looking for,
Curtis

This may be of help. I’ve created a helper method that overcomes the
problem in my applications:

  #Variation of in_place_editor_field, which add a plus sign in empty 
fields so there is something to select
  #Usage: item = object that field is a property of
  #       symbol = name of object represented as a symbol
  #       field = the name of the property/field to have edit function 
(symbol)
  #       user_can_edit = boolean. in_place_editor only available if 
true
  def in_place_editor_field_with_empty(item, symbol, field, 
user_can_edit)
    if user_can_edit
      output_text = String.new
      eef_id = 'eef_' + field.to_s + item.id.to_s
      if not item.send(field) or item.send(field).length < 1
        output_text += '<span class="empty_edit_field" id="' + eef_id + 
'">&#043;</span>'
      end
      output_text += in_place_editor_field symbol, field, {}, 
{:external_control => eef_id }
    else
      item.send(field)
    end
  end

It checks to see if the propert is empty and adds a plus sign instead of
a blank entry. Clicking on the plus sign opens the in place editor. You
can use the empty_edit_field class in CSS to control the appearance of
the plus sign. I think you should be able to replace symbol with a
function of item, but I haven’t worked out the right one yet.

I use it like this in a rhtml template:

<%= in_place_editor_field_with_empty(@stock, :stock, :purchase_order, user_logged_on?) -%>

where @stock is an item of stock
:stock a symbol for that item
:purchase_order is the name of the property being edited :
@stock.purchase_order
:user_logged_on? is another helper function that checks the users log on
status.

here’s a great extension that someone else wrote. I use it in my apps
now.
it should probably be part of the core:

On 12/20/06, Rob N. [email protected] wrote:

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


Mike W.
Web D.
UW-Eau Claire

I spent way too long trying to solve this problem. I was not having any
luck implementing any helpers or extensions in 2.3.2, and did not want
to set my database fields to “No data” or whatever to populate the blank
fields.

I therefore went with the old standby, dealing with the CSS through
javascript. Slightly hacky, but it works across browsers:

And the javascript:

function classes(cn, mom) { //from rnd_me at codingforums.com
if(!mom){mom=document;}
if(mom.getElementsByClassName){ return mom.getElementsByClassName(cn);
}
var rx = new RegExp(“(?:^|\s)” + cn+ “(?:$|\s)”);
var allT = mom.getElementsByTagName(“*”), allCN = [], ac=“”, i = 0, a;
while (a = allT[i=i+1]) {
ac=a.className;
if ( ac && ac.indexOf(cn) !==-1) {
if(ac===cn){ allCN[allCN.length] = a; continue; }
rx.test(ac) ? (allCN[allCN.length] = a) : 0;
}
}
return allCN;
}
function fixElements() {
var emptyInlineEdits = classes(“in_place_editor_field”);
for (var i=0; i < emptyInlineEdits.length; i++) {
if (emptyInlineEdits[i].innerHTML==‘’) {
emptyInlineEdits[i].innerHTML=‘_’;
}
}
}

I hope this can save someone some time!

Rob N. wrote:

  if not item.send(field) or item.send(field).length < 1

Small bug fix. This causes an error if propert isn’t a string, so:

if not item.send(field) or item.send(field).to_s.length < 1