Forum: Ruby on Rails Setting empty fields with in_place_editor_field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Raphael S. (Guest)
on 2006-03-22 22:32
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
Raphael S. (Guest)
on 2006-03-24 15:34
Does really nobody use in_place_editor_field like that, for setting a
previously empty field to some value?
Eric W. (Guest)
on 2006-03-24 16:02
(Received via mailing list)
On 3/24/06, Raphael S. <removed_email_address@domain.invalid> 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
Raphael S. (Guest)
on 2006-03-25 01:37
That's a simple yet wonderful idea -- I'll go with a dash. Thanks a lot
Eric!
Curtis Hatter (Guest)
on 2006-03-25 04:25
(Received via mailing list)
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 %>
<p>
  <span id="<%= column.name -%>"><b><%= column.human_name %>:</b></span>
  <%= in_place_editor_field :books, column.name, {}, {:external_control
=> column.name } -%>
</p>
<% 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 (Guest)
on 2006-03-25 04:32
(Received via mailing list)
Curtis Hatter wrote:

> <p>
> ":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
Rob N. (Guest)
on 2006-12-20 17:34
This may be of help. I've created a helper method that overcomes the
problem in my applications:
[code]
  #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
[/code]

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:

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

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.
Rob N. (Guest)
on 2006-12-20 19:21
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
Mike W. (Guest)
on 2006-12-20 21:15
(Received via mailing list)
here's a great extension that someone else wrote.  I use it in my apps
now.
it should probably be part of the core:
http://www.akuaku.org/archives/2006/08/in_place_ed...

On 12/20/06, Rob N. <removed_email_address@domain.invalid> wrote:
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Mike W.
Web D.
UW-Eau Claire
Ilana R. (Guest)
on 2009-08-06 21:31
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:

<body onload="fixElements();">

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!
This topic is locked and can not be replied to.