Ruby on Rails
InPlaceEditing (Version #24)

Called id for nil, which would mistakenly be 4—if you really wanted the id of nil, use object_id

If you’re receiving this error while using the in_place_editor_field method, chances are you’re iterating through a list of objects and you’re passing the variables to the method incorrectly.

To fix this problem, set the current iteration as an instance variable and you should be fine.

For example:

<% for person in @people %>
<% @person = person %>
<%= in_place_editor_field :person, :name %>

in_place_editor_field expects a symbol that points to an instance variable (that is, a variable prepended with an “at sign” or ”@”) as its first parameter, and the name of the database table column to update as the second parameter (this does not have to be defined as an instance variable—simply enter it as a symbol and it will work).

The reason this error occurs is because the definition of the method, in the java_script_macros_helper.rb file, tries to create a new InstanceTag, which expects an instance variable as well. Here is the source:

def in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {})
  tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)
  tag_options = {:tag => "span", :id => "#{object}_#{method}_#{tag.object.id}_in_place_editor", :class => "in_place_editor_field"}.merge!(tag_options)
  in_place_editor_options[:url] = in_place_editor_options[:url] || url_for({ :action => "set_#{object}_#{method}", :id => tag.object.id })
  tag.to_content_tag(tag_options.delete(:tag), tag_options) +
  in_place_editor(tag_options[:id], in_place_editor_options)
end

Since InstanceTag returns a nil object since its first parameter is incorrectly passed, the call to set the value of tag_options will also fail because tag.object.id is nil. And that’s why you’ll get that error.

If you’re frustrated with this and don’t want to set the variables all the time, another option is to use the in_place_editor method instead. You’ll have to set up your own span tag to receive the in-place-editing, but it’s an available work-around. —Adam B. Traver

Also keep in mind that java_script_macros_helper.rb will ignore :highlightcolor and :highlightendcolor. The following code should work, but has no real effect:

<%= in_place_editor_field( 'person', :name, {}, { :highlightcolor => '#ff0000' } ) %>

See

Called id for nil, which would mistakenly be 4—if you really wanted the id of nil, use object_id

If you’re receiving this error while using the in_place_editor_field method, chances are you’re iterating through a list of objects and you’re passing the variables to the method incorrectly.

To fix this problem, set the current iteration as an instance variable and you should be fine.

For example:

<% for person in @people %>
<% @person = person %>
<%= in_place_editor_field :person, :name %>

in_place_editor_field expects a symbol that points to an instance variable (that is, a variable prepended with an “at sign” or ”@”) as its first parameter, and the name of the database table column to update as the second parameter (this does not have to be defined as an instance variable—simply enter it as a symbol and it will work).

The reason this error occurs is because the definition of the method, in the java_script_macros_helper.rb file, tries to create a new InstanceTag, which expects an instance variable as well. Here is the source:

def in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {})
  tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)
  tag_options = {:tag => "span", :id => "#{object}_#{method}_#{tag.object.id}_in_place_editor", :class => "in_place_editor_field"}.merge!(tag_options)
  in_place_editor_options[:url] = in_place_editor_options[:url] || url_for({ :action => "set_#{object}_#{method}", :id => tag.object.id })
  tag.to_content_tag(tag_options.delete(:tag), tag_options) +
  in_place_editor(tag_options[:id], in_place_editor_options)
end

Since InstanceTag returns a nil object since its first parameter is incorrectly passed, the call to set the value of tag_options will also fail because tag.object.id is nil. And that’s why you’ll get that error.

If you’re frustrated with this and don’t want to set the variables all the time, another option is to use the in_place_editor method instead. You’ll have to set up your own span tag to receive the in-place-editing, but it’s an available work-around. —Adam B. Traver

Also keep in mind that java_script_macros_helper.rb will ignore :highlightcolor and :highlightendcolor. The following code should work, but has no real effect:

<%= in_place_editor_field( 'person', :name, {}, { :highlightcolor => '#ff0000' } ) %>

See