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?
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.
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.
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 +
'">+</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.
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.
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=’_’;
}
}
}