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.
77961c972c437d6801714f45c3f2cc3c?d=identicon&s=25 Raphael Schmid (rapha)
on 2006-03-22 21: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
77961c972c437d6801714f45c3f2cc3c?d=identicon&s=25 Raphael Schmid (rapha)
on 2006-03-24 14:34
Does really nobody use in_place_editor_field like that, for setting a
previously empty field to some value?
0f1fa6b6ea69391ae1d1f64b33cb77b8?d=identicon&s=25 Eric Wagoner (Guest)
on 2006-03-24 15:02
(Received via mailing list)
On 3/24/06, Raphael Schmid <raphael@schwarzschmid.de> 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
77961c972c437d6801714f45c3f2cc3c?d=identicon&s=25 Raphael Schmid (rapha)
on 2006-03-25 00:37
That's a simple yet wonderful idea -- I'll go with a dash. Thanks a lot
Eric!
5a90462f5f0940f09a76bf1e39bed595?d=identicon&s=25 Curtis Hatter (Guest)
on 2006-03-25 03:25
(Received via mailing list)
Raphael Schmid 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
5a90462f5f0940f09a76bf1e39bed595?d=identicon&s=25 Curtis Hatter (Guest)
on 2006-03-25 03: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
09e6ba06911a5781c04be8554f114aba?d=identicon&s=25 Rob Nichols (reggieb)
on 2006-12-20 16: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.
09e6ba06911a5781c04be8554f114aba?d=identicon&s=25 Rob Nichols (reggieb)
on 2006-12-20 18:21
Rob Nichols 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
6bc7fe7c135d444c9bde446c83855578?d=identicon&s=25 Mike Weber (Guest)
on 2006-12-20 20: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 Nichols <rails-mailing-list@andreas-s.net> wrote:
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Mike Weber
Web Developer
UW-Eau Claire
3daa7bee46137c8136cdbd7af1983f07?d=identicon&s=25 Ilana Reeves (ilanar)
on 2009-08-06 19: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.