# Company = params\[:person\].delete(:company) nil object error on nil.delete

**URL:** <https://www.ruby-forum.com/t/company-params-person-delete-company-nil-object-error-on-nil-delete/116039>\
**Category:** Rails\
**Created:** [October 1, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/company-params-person-delete-company-nil-object-error-on-nil-delete/116039 "2007-10-01T20:55:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![riles01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@riles01](https://www.ruby-forum.com/u/riles01)\
**Post date:** [October 1, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/company-params-person-delete-company-nil-object-error-on-nil-delete/116039/1 "2007-10-01T20:55:04Z")

</div>

I am using the Rails 2.0 pre-release and trying to replicate the  
method used by Jamis to create a company, person, and then display the  
nested xml using .to\_xml as shown here:  
[Buckblog: Web services, Rails-style](http://weblog.jamisbuck.org/2006/3/27/web-services-rails-style).  
However, I get the following error:

```

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.delete
  
```

I can see that all my parameters are passing in correctly from the  
form:

```

{"company"=>{"name"=>"company"},
 "commit"=>"Create",
 "title"=>"wacky",
 "authenticity_token"=>"7f3e6aaaba34fd8146ce244c2038797ebb1f1593",
 "first_name"=>"Ryan",
 "last_name"=>"Riley"}
  
```

I was pretty certain this came in as an array, so I’m not sure why I’m  
getting an error saying it is not. Here’s the relevant controller and  
view code:

PeopleController.rb

```

  # POST /contacts/people
  # POST /contacts/people.xml
  def create
    company = params[:person].delete(:company)
    @company = Company.find_or_create_by_name(company[:name])
    @person = @company.people.build(params[:person])

    respond_to do |format|
      if @person.save
        flash[:notice] = 'Person was successfully created.'
        format.html { redirect_to(contacts_person_path(@person)) }
        format.xml { render :xml => @person.to_xml( :include =>
@company ), :status => :created, :location => @person }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @person.errors, :status
=> :unprocessable_entity }
      end
    end
  end
  
```

new.html.erb

```

New person

<%= error_messages_for :person %>

<% form_tag([:contacts, @person]) do %>
  
    First name
    <%= text_field_tag :first_name %>
  

  
    Last name
    <%= text_field_tag :last_name %>
  

  
    Title
    <%= text_field_tag :title %>
  

  
    Company
    <%= text_field_tag "company[name]" %>
  

  
    <%= submit_tag "Create" %>
  
<% end %>

<%= link_to 'Back', contacts_people_path %>
  
```

---

<div class="post-metadata">

**Author:** ![riles01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@riles01](https://www.ruby-forum.com/u/riles01)\
**Post date:** [October 1, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/company-params-person-delete-company-nil-object-error-on-nil-delete/116039/2 "2007-10-01T22:13:53Z")

</div>

It doesn’t look like your person params are in params[:person] at all  
(you’re just using text\_field\_tag :first\_name, and so on, which has no  
idea that first\_name is part of person), so params[:person] is null

Fred

---

<div class="post-metadata">

**Author:** ![riles01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@riles01](https://www.ruby-forum.com/u/riles01)\
**Post date:** [October 1, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/company-params-person-delete-company-nil-object-error-on-nil-delete/116039/3 "2007-10-01T23:57:54Z")

</div>

Thanks, Fred. I was using form\_for, switched to form\_tag, and then  
switched back to form\_for. Looking more closely at what was sent,  
the :company parameter is not part of the :person parameter anyway, so  
I just had to change the first line in my controller to:

```

  def create
    company = params[:company]
    @company = Company.find_or_create_by_name(company[:name])
    @person = @company.people.build(params[:person])
    ...
  end
  
```

Works like a champ, now. Thanks!

On Oct 1, 3:02 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
