I have an iOS project that sends a Person class with one attribute name which is “data” to the server. The server sees it, but sets it to null.
Processing PeopleController#create (for 127.0.0.1 at 2012-01-13 03:55:46) [POST]
Parameters: {"name"=>"data"}
Person Create (0.4ms) INSERT INTO "people" ("created_at", "updated_at", "name")
VALUES('2012-01-13 11:55:46', '2012-01-13 11:55:46', NULL)
Completed in 27ms (View: 1, DB: 0) | 200 OK
It’s a scaffold generated Rails app with Person has_many dogs and dogs belongs_to person.
The following is the scaffold generated create method:
def create
@person = Person.new(params[:person])
respond_to do |format|
if @person.save
flash[:notice] = 'Person was successfully created.'
format.html { redirect_to(@person) }
format.xml { render :xml => @person }
format.json { render :json => @person }
else
format.html { render :action => "new" }
format.xml { render :xml => @person.errors, :status => :unprocessable_entity }
format.json { render :json => @person.errors, :status => :unprocessable_entity }
end
end
end
What am I doing wrong? Do I need to write a custom JSON parser?
I use Rails version 2.3.4 with Ruby 1.8.7.
You are building your model using
params[:person]. Or, you can see that the parameters received are{"name"=>"data"}, when they should be{"person" => {"name"=>"data"}}