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. However, I get the following error:
<pre> <code> 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 </code> </pre>
I can see that all my parameters are passing in correctly from the form:
<pre> <code> {"company"=>{"name"=>"company"}, "commit"=>"Create", "title"=>"wacky", "authenticity_token"=>"7f3e6aaaba34fd8146ce244c2038797ebb1f1593", "first_name"=>"Ryan", "last_name"=>"Riley"} </code> </pre>
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 <pre> <code> # 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 </code> </pre>
new.html.erb <pre> <code> <h1>New person</h1>
<%= error_messages_for :person %>
<% form_tag([:contacts, @person]) do %> <p> <b>First name</b><br /> <%= text_field_tag :first_name %> </p>
<p> <b>Last name</b><br /> <%= text_field_tag :last_name %> </p>
<p> <b>Title</b><br /> <%= text_field_tag :title %> </p>
<p> <b>Company</b><br /> <%= text_field_tag "company[name]" %> </p>
<p> <%= submit_tag "Create" %> </p> <% end %>
<%= link_to 'Back', contacts_people_path %> </code> </pre>